How to check your public IP using PowerShell
Theoretically you can't reliably lookup your public IP address inside your local machine. You might be behind a proxy, using a VPN and other situation that translate your public exposure. The best way to reliably lookup your public IP address is to ask an outside service to tell you what is the IP address it sees when you send a request. Luckily there is a service that can do that without fuss. You only need to run the following one liner.
Invoke-WebRequest "https://checkip.amazonaws.com"
This will give you a response like the following.
StatusCode : 200
StatusDescription : OK
Content : 151.105.107.123
RawContent : HTTP/1.1 200 OK
X-Bst-Request-Id: jNCsWj:NT8F:333748
X-Bst-Info: t=1727069419,h=18x,p=82501_3888:2_12139,u=1273770767,c=2339,v=7.11.56
Date: Mon, 23 Sep 2024 05:30:18 GMT
Server: nginx
Vary: Orig…
Headers : {[X-Bst-Request-Id, System.String[]], [X-Bst-Info, System.String[]], [Date,
System.String[]], [Server, System.String[]]…}
The content of the message is your public IP address. To make it simple to read we can select the Content. In other words:
Invoke-WebRequest "https://checkip.amazonaws.com" | select -ExpandProperty Content
The above command will give your public IP address without fuss, just like promised.