How to Get Public IP address using PowerShell

You can easily find Public IP address (IPv4 and IPv6) assigned to your computer using PowerShell commands. However, if you have access to any web browser, you can also simply type What is my IP address in google or bing and easily get the Public IP address details that way as well.

There are two PowerShell cmdlets which will help us retrieve this information Invoke-WebRequest and Invoke-RestMethod. You can use any of these cmdlets. These cmdlets along with below web services can fetch the Public IP address and also other metadata.

  • https://api.ipify.org/
  • https://ipinfo.io/ip
  • https://ifconfig.me/ip
  • https://icanhazip.com
  • https://ident.me
  • Public IP Address metadata
    • 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

Fetching Private IP address (Internal) of your local computer is easy and can be retrieved by typing IPconfig or Get-NetIPAddress | Select IPAddress commands.

Get Private IP Address using PowerShell

Get the Public IP Address using PowerShell

To retrieve the Public IP address (External) assigned to your device, you can use any of the above web services/endpoints and use it with either Invoke-WebRequest or Invoke-RestMethod.

  • Open PowerShell console on your device.
  • Copy and paste any of 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
(curl ifconfig.me).content
(Invoke-WebRequest ident.me).content
(Invoke-WebRequest ipecho.net/plain).content
(Invoke-WebRequest ifconfig.me/ip).content
(Invoke-WebRequest ipconfig.me).content
(Invoke-RestMethod -uri "https://api.ipify.org/")
Resolve-DnsName -Name myip.opendns.com -Server resolver1.opendns.com | Select IP4address
Resolve-DnsName -Name myip.opendns.com -Server resolver1.opendns.com | fl *
nslookup myip.opendns.com resolver1.opendns.com

Invoke-WebRequest

(Invoke-WebRequest -uri "https://api.ipify.org/").Content 
Invoke-WebRequest to get Public IP

or simply use this quick command

(curl ifconfig.me).content
Short command to get Public IP

Invoke-RestMethod

You can get the same results using Invoke-RestMethod cmdlet as well. Let’s run it and check the results:

Invoke-RestMethod

(Invoke-RestMethod -uri "https://api.ipify.org/")
Invoke-RestMethod to get Public IP

Retrieve metadata about the Public IP address

Copy and Paste below Powershell code 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

Open the PowerShell console and execute the Get-PublicIP.ps1 script. It will retrieve all the Information about the Public IP addresses.

Powershell script to get Public IP Information

OpenDNS Web Service

Another way to get Public IP address is by using OpenDNS web service. Execute below command on PowerShell console to get the public IP address information.

Resolve-DnsName

Resolve-DnsName -Name myip.opendns.com -Server resolver1.opendns.com | Select IP4address

To get more information about Public IP address, execute below command

Resolve-DnsName | get more information

Resolve-DnsName -Name myip.opendns.com -Server resolver1.opendns.com | fl *

Alternative command | nslookup

nslookup myip.opendns.com resolver1.opendns.com

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:

Get Public IPv6 Address

(Invoke-RestMethod -Uri "https://ifconfig.me/ip")
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")

Invoke-RestMethod vs 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.

Read Next

Leave a Comment