≡ Menu

PowerShell: Find my public IP

Every computer requires a public IP address to connect to the internet. Some individuals/organizations buys public IP addresses from their Internet Service Provider(ISP) and some uses ISPs proxy to connect to the internet. In any case, traffic(web) from your computer need to flow through a public IP address in order to communicate with web sites/servers/computers that are in the internet. This article will show a quick PowerShell way of finding this public IP address.

There are many websites like whatismyip.com etc which can give you this information. It can be queried from any programming languages using REST API provided as well. For demonstration, I am using http://ipinfo.io services and I will query it using Invoke-RestMethod (irm in short) cmdlet in PowerShell.

Invoke-RestMethod -Uri "http://ipinfo.io"

And the output will look similar to below.

I have hidden the information intentionally, but when you run this command from PowerShell window on your computer, you should see the IP address and other details. You can access these details individually as well if you like.

#To get IP address only
(Invoke-RestMethod -Uri "http://ipinfo.io").IP

#To get City Name
(Invoke-RestMethod -Uri "http://ipinfo.io").City

#To get Country Name
(Invoke-RestMethod -Uri "http://ipinfo.io").Country

One thing to keep in mind here is, the City & Country information may look incorrect sometimes as it is totally depends on how your internet provider is routing the traffic. Also if your computer is using any VPN to connect to internet, these details will change as well.

Hope this information is useful.

{ 0 comments… add one }

Leave a Comment