Get My 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 Read on for more background and details…





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 several websites that offer public IP discovery services. 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!


Happy scrapeless scripting…

Leave a Reply

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