PowershellWindows

How to Get My Public IP Address Using PowerShell

By January 4, 2019 October 30th, 2019 No Comments

Question

Hi, 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 websites 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.

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

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.

Leave a Reply