Powershell can be used to fetch public IP address information from one of the external websites. The quick and short method involves invoking the web request method to one of the external websites that maintain the IP address detector API or software.
We look at multiple ways by which you can find the public IP address using Powershell. For any of these commands to work, you will need to make sure that the access to these websites is whitelisted through your firewall or peripheral network if any.
Before we proceed with the actual commands, it may be pertinent to reiterate that the IP address information is imported from third-party websites. At any point in time, you may run into access or functionality issues with any of these third-party websites. So, it is imperative that we have more than one IP feed website for collecting the public IP address of our network device (like a router) or an endpoint that has a static IP allocated to it.
Essentially, we need:
- access to third-party IP database sites
- more than one site needs to be used so that we can import the IP address information from alternate sites if the primary one goes down
Also, there is another point worth noting. We may have an IPv4 and IPv6 IP address. This may be true for Internet connections. The Web Application Firewall software (for example Cloudflare) may work on the IPv6 IP address. Hence, you may want to have access to IPv4 and IPv6 IP addresses at all times.
Find IPv6 public IP address using Powershell
For finding IPv6 version public IP address of your endpoint or network device, we will make use of the IP database hosted at the icanhazip.com website. We will send HTTP and HTTPS requests to the Restful services offered by the site and get the IPv6 IP address of the endpoint, network device or computer.
Invoke-RestMethod -Uri “https://icanhazip.com”
The command above produces the output displayed in the screenshot below.
Apart from using the RestMethod, we can also use a web request to the REST API services offered by the third-party websites. So, we can also use the WebRequest method to organize the same IP information. The web request command will change to the following.
(Invoke-Webrequest -Uri “https://icanhazip.com”).Content
When we invoke the RESTful services through the webrequest method, we get the output represented in the screenshot below.
Find public IP address in IPv4 format in Powershell
IPv4 IP address information for the network device, endpoint or computer can be found through the RestMethod or WebRequest methods. We will look at both options and use a third-party website to get the IP information.
For getting the public IPv4 IP address, we will use the ipify.org website. The exact command uses the Web Request method and is shared below:
Invoke-WebRequest -uri “https://api.ipify.org/”
The command output is represented in the screenshot below. You will see that the command output is more detailed. I like the fact that the IP address in IPv4 format also carries the timestamp. The timestamp entry corresponds to the time when we pulled the IP information from the ipify.org website.
Timestamps can be very useful, especially when you are troubleshooting any network related issues.
Apart from the ipify.org website, we can also use the ipinfo.io website for pulling the public IP address information in IPv4 format. We can use the RestMethod or the WebRequest method to ipinfo.io website and import the public IP address data.
The following command will fetch your system’s public IPv4 IP address from the ipinfo.io website:
Invoke-RestMethod -Uri “https://ipinfo.io/ip”
The output of this command lists the public IP address in IPv4 format as displayed in the screenshot below.
We can also use the Web Request method to fetch the IP address information from the ipinfo.io website. The command that invokes the web request to pull the IP information is stated hereunder.
Invoke-WebRequest -uri “https://ipinfo.io/ip”
The command output for the web request method is shared below in the screenshot. You can see that the IP information also shares the timestamp at which the IP address data was supplied by the ipinfo.io site.
You could use any of these RestMethod or WebRequest methods for pulling the public IP address information from the IP database websites.
Depending on your exact requirements, you may use different sites for IPv6 or IPv4 IP address data. On a similar basis, depending on the details needed, you may choose the WebRequest or the RestMethod approach through the Invoke command.
We are sorry that this post was not useful for you!
Let us improve this post!
Tell us how we can improve this post?
Rajesh Dhawan is a technology professional who loves to blog about smart wearables, Cloud computing and Microsoft technologies. He loves to break complex problems into manageable chunks of meaningful information.