How to Get Public IP address using Powershell

This blog post will show how you can easily retrieve your computer’s 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 do not have access to the GUI way of getting Public IP address Info.

If you can access a web browser and Internet connection, finding the public IP address information on your Internet connection becomes easy. You can type What is my IP address on Google, and it will provide that Information.

Today, we will explore command line options to retrieve Public IP addresses. Not only will we be able to retrieve Public IP address information but also the following:

  • City – For example, London
  • Region – For example, England
  • Country – For instance: GB
  • Location Coordinates of the IP
  • Org
  • Postal
  • Timezone – For example, Europe/London
  • readme

Get the 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.

However, to retrieve a Public IP address (External), we will need to use a third-party web-based service or endpoints that can be used to retrieve a Public IP address when you make an HTTP request.

Please find below a list of third-party services that can be used to retrieve Public IP addresses:

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 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 the Start Menu and Launch it.
  • Copy and paste the below command into the Powershell console and press Enter.
  • It will output the Public IP Address on the console.
(Invoke-WebRequest -uri "https://api.ipify.org/").Content 
Powershell command to get Public IP Address
Powershell command to get Public IP Address

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/")
Powershell command to get Public IP Address
Powershell command to get Public IP Address

You can also retrieve more information about Public IP Addresses. Please follow the below steps:

  1. Copy and Paste the Powershell code below into a notepad and Save it as Get-PublicIP.ps1.

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
  1. Open the Powershell console and execute the Get-PublicIP.ps1 script. It will retrieve all the Information about the Public IP addresses, as you can see below screenshot:
Powershell command to get Public IP Address
Powershell command to get Public IP Address

Retrieve Public IP Address (IPv6) using Powershell

If you are looking to retrieve the Public IP address (IPv6) then you can execute the following commands on the Powershell console:

(Invoke-RestMethod -Uri "https://ifconfig.me/ip")
Retrieve Public IP Address (IPv6) using Powershell
Retrieve Public IP Address (IPv6) using Powershell

You can also use the Powershell commands below to retrieve the 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:

CategoryInvoke-RestMethodInvoke-WebRequest
Primary Use CaseDesigned for RESTful APIs and structured data.General-purpose web requests and content retrieval.
Response HandlingAutomatically parses structured data (e.g., JSON, XML).Returns a detailed response object with various properties.
Content TypeSuited for structured data formats.Suitable for various content types, including unstructured data.
UsageInteracting with REST APIs and services.Content retrieval, web scraping, and more.

Leave a Comment