Every so often we run into situations we need to grab a public IP of a Windows server or a windows PC. There are several ways of doing this but my preferred method leverages PowerShell, specifically the Invoke-WebRequest CMDLET. It sends HTTP, HTTPS, FTP, and FILE requests to a web page or service and responds with elements that you have configured it to capture such as links, images etc.
To obtain the public IP via PowerShell run the simple one-liner below:
PS C:\> (Invoke -WebRequest -uri "http://ipecho.net/plain" ).Content |
Running this command will successfully show your public IP:
There are numerous other services you can call to get the public IP, a few are below:
http://ipinfo.io/ip http://icanhazip.com http://ident.me http://smart-ip.net/myip
Simply replace http://ipecho.net/plain in the PowerShell command above with one of the URLs above and you should get your public IP back in exactly the same way.
Credits: Microsoft, Invoke-WebRequest