Get Your Public IP Address Using PowerShell

Getting your public IP address programmatically can be very useful. Especially if you need to know when your internet service provider (ISP) has changed your dynamic IP address. Here is how to get your public IP address using PowerShell: (Invoke-WebRequest Another way to get your public IP address using PowerShell is by using Invoke-RestMethod. For example, (Invoke-RestMethod Read on for more background and details…




Other URLs exist which will provide you with your “raw” IP address without having to scrape through page tags:


Some background info

In Windows you can run ipconfig and, in Linux, ifconfig, to get your IP address. If your computer is not sitting behind a router, ipconfig and ifconfig will return the public (internet) IP address provided by your Internet Service Provider (ISP). This IP address can also be referred to as the Wide Area Network (WAN) address. So far so good…

Most households and businesses, however, employ routers, which use Network Address Translation (NAT). NAT allows multiple devices to share a common “public”, or WAN, IP address. Unfortunately, when behind a router, the IP address you would get from ipconfig or ifconfig is the internal home or office network IP address of your computer, which is only visible to other internal home or office network devices.

When behind a router, in order to get the public IP address, you can log into your router and review the configuration. If this is not possible, you would need to get your public IP information from some other device that knows it. This device would essentially have to reside outside of your internal network, where your router’s external public network interface is visible.

There are many services that offer public IP discovery. Just Google “What Is My IP”. Google may even provide it directly in its search results. However, getting your public IP address programmatically may not be as easy. The actual IP address value is normally buried in a bunch of HTML code. This, then, requires scraping. Not fun.

Well, here’s an easy way to get your public IP address using PowerShell…

How do I get my public IP address Using PowerShell?


Note: Please disregard the .555. It’s for demonstration purposes only…As you can see, it’s very simple. We use Invoke-WebRequest against Again, we can also run Invoke-WebRequest against other “What’s My IP” type URLs, but those usually have to be scraped for the IP address value. Please note Invoke-WebRequest requires PowerShell version 3.0 or later.

You can accomplish this using any language that can perform a web request. It doesn’t have to be PowerShell!

Another easy way to retrieve your public IP address using PowerShell is by running the Invoke-RestMethod command against or That is, (Invoke-RestMethod or (Invoke-RestMethod You can also get other IP information such as hostname, city, country, and geolocation (latitude and longitude). See for more info.

$ip = Invoke-RestMethod


Get My Public IP using PowerShell - RestMethod

Happy scrapeless scripting…

3 thoughts on “Get Your Public IP Address Using PowerShell

  1. nice idea, however unfortunately not working anymore since IE engine seems to be needed but not existing in latest Windows releases anymore.

  2. Hello MW and thanks for your comment.

    Running Invoke-WebRequest with the -UseBasicParsing switch should work around the issue. For example, (Invoke-WebRequest -UseBasicParsing).content. Another alternative is if IE had never been launched on the machine, then you can initialize IE by modifying the registry – in PowerShell like so:

    Set-ItemProperty -Path “HKLM:\SOFTWARE\Microsoft\Internet Explorer\Main” -Name “DisableFirstRunCustomize” -Value 2

    Hope it helps!

Leave a Reply

Your email address will not be published. Required fields are marked *