Table of Contents
In this blog post, we will see how you can easily retrieve Public IP address Information using Powershell. There are many scenarios where you would need to retrieve this information e.g. If you are creating an automated script or CI/CD pipeline in Azure DevOps or you do not have access to the GUI way of getting Public IP address Info.
If you have access to a web browser and Internet connection, It becomes easy to find the Public IP address Information of your Internet connection. You can simply type What is my IP address on Google and it will provide that Information to you.
Today, we will explore command line options to retrieve Public IP address. Not only we will be able to retrieve Public IP address Information but also below Information as well:
- City – For example: London
- Region – For example: England
- Country – For example: GB
- Location Coordinates of the IP
- Org
- Postal
- Timezone – For example Europe/London
- readme
Get Public IP Address using Powershell
Fetching Private IP address (Internal) of your local computer is easy and can be retrieved by typing IPconfig
or Get-NetIPAddress | Select IPAddress
commands.
But when it comes to retrieving Public IP address (External), we will need to use a third-party web-based service or endpoints that can be used to retrieve Public IP address when you make an HTTP request.
Please find below list of third-party services that can be used to retrieve Public IP address:
- https://api.ipify.org/
- https://ipinfo.io/ip
- https://ifconfig.me/ip
- https://icanhazip.com
- https://ident.me
You can use any of the two PowerShell cmdlets to fetch public IP information from web services: the first one is Invoke-RestMethod
and the second one is Invoke-WebRequest
.
Both cmdlets will work fine for retrieving the required information. However, if you’re curious about knowing the differences between these two cmdlets, I’ve outlined the points at the end of the blog post.
Let’s run a command using Invoke-WebRequest
PowerShell cmdlet and see the results:
- Search for Powershell in Start Menu and Launch it.
- Copy and paste below command into Powershell console and press Enter.
- It will output Public IP Address on the console.
(Invoke-WebRequest -uri "https://api.ipify.org/").Content
You can get the same results using Invoke-RestMethod
cmdlet as well. Let’s run it and check the results:
(Invoke-RestMethod -uri "https://api.ipify.org/")
You can also retrieve more information about Public IP Address. Please follow below steps:
- Copy and Paste below Powershell code into a notepad and Save it as Get-PublicIP.ps1
# Fetch IP information from ipinfo.io $ipInfo = Invoke-RestMethod http://ipinfo.io/json # Create an object to format the data as a table $output = @{ "IP Address" = $ipInfo.ip "Hostname" = $ipInfo.hostname "City" = $ipInfo.city "Region" = $ipInfo.region "Country" = $ipInfo.country "Location" = $ipInfo.loc "Organization" = $ipInfo.org "Postal Code" = $ipInfo.postal "Readme" = $ipInfo.readme } # Display the formatted table $output | Format-Table -AutoSize
- Open Powershell console and execute Get-PublicIP.ps1 script. It will retrieve all the Information about Public IP address as you can see in below screnshot:
Retrieve Public IP Address (IPv6) using Powershell
If you are looking to retrieve Public IP address (IPv6) then you can execute the following commands on Powershell console:
(Invoke-RestMethod -Uri "https://ifconfig.me/ip")
You can also use any of below Powershell commands as well which can retrieve Public IP Address IPv6 address.
(Invoke-RestMethod -Uri "https://icanhazip.com")
(Invoke-RestMethod -Uri "https://ident.me")
Difference between Invoke-RestMethod and Invoke-WebRequest
Please find below the difference between Invoke-RestMethod and Invoke-Webrequest Powershell cmdlets:
Category | Invoke-RestMethod | Invoke-WebRequest |
---|---|---|
Primary Use Case | Designed for RESTful APIs and structured data. | General-purpose web requests and content retrieval. |
Response Handling | Automatically parses structured data (e.g., JSON, XML). | Returns a detailed response object with various properties. |
Content Type | Suited for structured data formats. | Suitable for various content types, including unstructured data. |
Usage | Interacting with REST APIs and services. | Content retrieval, web scraping, and more. |