In this article, I will show you how to get public IP address using PowerShell. Getting your public IP address programmatically can be very useful in situations when you don’t have a browser installed or you are on the Windows core operating system.
There are multiple ways to find public IP address of a computer, and you are probably aware that there are numerous websites that can find public IP addresses. When you visit these websites, they reveal your public IP address. However, getting a public IP address with PowerShell would be very fascinating. Yes, you can get public IP address of the computer on which you are working using PowerShell commands.
Most of us know that you can get IP address of the system by running a simple command. For Windows machines, you must run ipconfig command and for Linux, it’s ifconfig command.
If you have an internet connection from your ISP, the system would be usually behind a router or a modem. Using router’s administration page, you can still find your public IP address. You need to log in to your router and review the configuration.
How to Get Public IP Address Using PowerShell
There are two ways to get public IP address using PowerShell on any computer:
- Using the (Invoke-WebRequest -uri “https://api.ipify.org/”).Content command.
- Using the Invoke-RestMethod. For example, (Invoke-RestMethod -Uri (‘https://ipinfo.io/’).
We will try both these PowerShell commands to find the public IP address of the computer. Run the below PowerShell command to find the Public IP address.
(Invoke-WebRequest -uri "https://api.ipify.org/").Content
Wait a second I just got a weird error when I ran the above command. Nothing to worry here, it’s just that you haven’t opened the Internet Explorer on your computer yet. The command says Internet Explorer engine is not available, or its first launch configuration is incomplete. Seems like after installing the operating system, the Internet Explorer application was never launched.
Invoke-WebRequest : The response content cannot be parsed because the Internet Explorer engine is not available, or Internet Explorer's first-launch configuration is not complete. Specify the UseBasicParsing parameter and try again.
Note: The Invoke-WebRequest requires PowerShell version 3.0 or later. Ensure you update the PowerShell on your computer to the latest version before running the above commands.
Important: The Internet Explorer 11 is going out of support on June 15, 2022. You won’t be getting any updates for Internet Explorer once it goes out of Support. Plan to disable Internet Explorer 11 on computers using GPO.
So to fix the above error, open Internet Explorer once and close it. Run the command again, and you should see no errors.
In the above command, we used api.ipify.org to get public IP from PowerShell. In addition to that uri, there are several other options available. You can use any of the following sites to query the public IP address:
Useful Article: How to Unlock User Accounts with PowerShell
Furthermore, it is possible to get more details about your internet service provider. You can run the below PowerShell command to find out details such as.
- IP Address
$ip = Invoke-RestMethod http://ipinfo.io/json $ip.ip $ip.hostname $ip.city $ip.region $ip.country $ip.loc $ip.org $ip.postal $ip.readme
Invoke-RestMethod -Uri ('https://ipinfo.io/')