Getting Geo Location of Any IP Address or Hostname in PowerShell

0 is a free web app that provides a public HTTP API for software developers to search the geo-location of any given IP address or hostname. It uses a database of IP addresses associated with cities along with some other relevant information such as time zone, latitude, and longitude, etc. You can supply a hostname or IP address to get the result in 3 formats, CSV, XML, and JSON.

The syntax is simply like this, and you can use it right in any of the browsers you use.{format}/{IP_or_hostname}
in which, format can be one of "CSV", "XML", or "JSON"

For example, to get Geolocation of our web server in XML in a browser, you do: - 2015-04-23 11_33_30

Now let’s see how we can do it in PowerShell way.

Invoke-RestMethod is a PowerShell cmdlet that deals with HTTP/HTTPS requests related to Representational State Transfer (REST) web services that return richly structured data, such as XML or JSON. It’s a perfect candidate to perform the call and display the result in a nicely formatted way.

So, to get the geolocation info of our web server in PowerShell, you can just run the following cmdlet in the PowerShell window.

Invoke-RestMethod -Method Get -Uri | Select-Object -Property InnerXML | Out-Gridview

Invoke-RestMethod -Method Get -URI - 2015-04-23 14_06_26

To put together a PowerShell script that checks any given IP address and hostname, try the following codes.

#requires -Version 3 
$source = [string]$args[0] 
$infoService = "$source" 
$geoip = Invoke-RestMethod -Method Get -URI $infoService 

Windows PowerShell ISE - 2015-04-23 14_11_04

You can also download the same script below to save some of your times. Download it to your local computer and run it in PowerShell window with the following usage:

.\ipgeo.ps1 {ip_or_hostname}

An alternative

Nekudo is another free IP geolocation API that we can use to archive this. The HTTP Get request schema of using the service is something like this:{ip}

Then you can use the Invoke-RestMethod cmdlet piped with Select-Object to retrieve the information.

Invoke-RestMethod -Method Get -Uri "{ip}" | Select-Object -ExpandProperty Country -Property City, IP, Location

Here is where Google’s public DNS service lives:


If you would like to find out the geolocation of a hostname, you will need to resolve the DNS info and save it in a variable first.

$IPAddress = [Net.Dns]::GetHostByName('domain-name').AddressList.IPAddressToString

Here is where Microsoft’s website lives:


Furthermore, since the dataset you retrieved from Nekudo’s API includes the latitude and longitude coordinates, you can find out where it actually using Google Maps.

To wrap it all up, the following code finds out the geolocation information about your public IP and opens it up in Google Maps.

$ip = Invoke-RestMethod -Uri
$geo = Invoke-RestMethod -Uri "$IP"
$latitude = $geo.Location.Latitude
$longitude = $geo.Location.Longitude 
$url = "$latitude,$longitude,12z"
Start-Process -FilePath $url

Another alternative is another web service that you can use the query the information about the IP. The following command returns the detail geo-location information about your current IP address.

Invoke-RestMethod -Uri ''

To query the info about a specific IP address, add /{ipaddress} at the end. For example, the following command reveals the geo-location information about Google Public DNS.

Invoke-RestMethod -Uri ''




Please enter your comment!
Please enter your name here