Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2012
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu
  • Home
  • About

Windows OS Hub

  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2012
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu

 Windows OS Hub / PowerShell / How to Get My Public IP Address Using PowerShell

December 6, 2018 PowerShell

How to Get My Public IP Address Using PowerShell

Hi, admin! In one of my PowerShell scripts, I needed to determine the current external IP address of the Windows computer from the command line or (best) with some simple PowerShell function. There are quite a few web sites where you can find out your public IP address, but I don’t understand how to access them from my PoSh script and return the data from the web page.

Answer

That’s right, to find out your external IP address, you can use any online service. You can get data from an external web page from PowerShell using the Invoke-WebRequest cmdlet (an example of parsing and scraping a web pages using Invoke-WebRequest).

You can parse the page of any of the popular sites to check the external IP address, but it is easier to use any of the services that contain only the ip address (in the form of plain-text).

You can use the following sites:

  • http://ipinfo.io/ip
  • http://ifconfig.me/ip
  • http://icanhazip.com
  • http://ident.me
  • http://smart-ip.net/myip

For example, to find out your current external IP address, from which you access the Internet, open the PowerShell console and run the command:

(Invoke-WebRequest -uri "http://ifconfig.me/ip").Content

As you can see, the command successfully returned to the PoSh console the external IP address from which the connection came.

Or even you can get your GeoIP data (such a country, city, region, and GPS coordinates).

Invoke-RestMethod -Uri ('http://ipinfo.io/'+(Invoke-WebRequest -uri "http://ifconfig.me/ip").Content

Tip. If you access the Internet through a proxy server, then you can authorize on it from PowerShell using this guide.

You should understand that in most cases the resulting IP will not be the real static “white” IP of your computer. In most cases, this will be either the external IP address of the router (when NAT is used), the dynamic IP address issued by provider or the proxy server address.

4 comments
5
Facebook Twitter Google + Pinterest
previous post
Remote IIS Management in Windows Server 2016/2012 R2
next post
Error 0x80073CFA: Can’t Uninstall Apps using Remove-AppxPackage in Windows 10

Related Reading

Using Previous Command History in PowerShell Console

January 31, 2023

How to Install the PowerShell Active Directory Module...

January 31, 2023

Finding Duplicate E-mail (SMTP) Addresses in Exchange

January 27, 2023

How to Disable or Uninstall Internet Explorer (IE)...

January 26, 2023

How to Delete Old User Profiles in Windows?

January 25, 2023

4 comments

Genie September 23, 2020 - 2:25 am

Hi, great information! Just a slight correction to you second invocation with gps data, the command is missing the closing bracket )
Invoke-RestMethod -Uri (‘http://ipinfo.io/’+(Invoke-WebRequest -uri “http://ifconfig.me/ip”).Content)

Reply
Sheldon October 25, 2021 - 3:05 pm

_http://ident.me currently has been hacked and contains a trojan/malware as per Malwarebytes – 10/25/2021

Reply
Pierre Carrier February 17, 2022 - 1:27 pm

@sheldon that’s mistake on their part, there was no hack or malware.

Reply
Pixel Encounter March 7, 2022 - 3:39 pm

I would recommend using the parameter -UseBasicParsing as well, (Invoke-WebRequest -uri “http://ifconfig.me/ip” -UseBasicParsing).Content;

Reply

Leave a Comment Cancel Reply

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMWare
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • Configure User’s Folder Redirection with Group Policy

    February 3, 2023
  • Using Previous Command History in PowerShell Console

    January 31, 2023
  • How to Install the PowerShell Active Directory Module and Manage AD?

    January 31, 2023
  • Finding Duplicate E-mail (SMTP) Addresses in Exchange

    January 27, 2023
  • How to Delete Old User Profiles in Windows?

    January 25, 2023
  • How to Install Free VMware Hypervisor (ESXi)?

    January 24, 2023
  • How to Enable TLS 1.2 on Windows?

    January 18, 2023
  • Allow or Prevent Non-Admin Users from Reboot/Shutdown Windows

    January 17, 2023
  • Fix: Can’t Extend Volume in Windows

    January 12, 2023
  • Wi-Fi (Internet) Disconnects After Sleep or Hibernation on Windows 10/11

    January 11, 2023

Follow us

woshub.com
  • Facebook
  • Twitter
  • RSS
Popular Posts
  • Managing Printers and Drivers with PowerShell in Windows 10 / Server 2016
  • Active Directory Dynamic User Groups with PowerShell
  • Assign Multiple IP Addresses (Aliases) to a Single NIC
  • How to Measure Storage Performance and IOPS on Windows?
  • Using PowerShell to View and Change BIOS Settings
  • Disks and Partitions Management with Windows PowerShell
  • How to Create a RAM Disk on Windows Server?
Footer Logo

@2014 - 2023 - Windows OS Hub. All about operating systems for sysadmins


Back To Top