Getting Geo Location of Any IP Address or Hostname in PowerShell

FreeGeoIP.net 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.

http://freegeoip.net/{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:

freegeoip.net_xml_www.nextofwindows.com - 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 http://freegeoip.net/xml/www.nextofwindows.com/ | Select-Object -Property InnerXML | Out-Gridview

Invoke-RestMethod -Method Get -URI http___freegeoip.net_xml_www.nextofwindows.co - 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 = "http://freegeoip.net/xml/$source" 
$geoip = Invoke-RestMethod -Method Get -URI $infoService 
$geoip.Response

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:

http://geoip.nekudo.com/api/{ip}

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

Invoke-RestMethod -Method Get -Uri "http://geoip.nekudo.com/api/{ip}" | Select-Object -ExpandProperty Country -Property City, IP, Location

Here is where Google’s public DNS service lives:

powershell-ip-geolocation-with-nekudo

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:

powershell-ip-geolocation-with-nekudo-from-a-hostname

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 http://checkip.amazonaws.com/
$geo = Invoke-RestMethod -Uri "http://geoip.nekudo.com/api/$IP"
$latitude = $geo.Location.Latitude
$longitude = $geo.Location.Longitude 
$url = "https://www.google.com/maps/preview/@$latitude,$longitude,12z"
Start-Process -FilePath $url

Another alternative

IPInfo.io 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 'http://ipinfo.io'

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 'http://ipinfo.io/8.8.8.8'

powershell-geolocation-with-ipinfo

Kent Chen

Microsoft MVP, IT Professional, Developer, Geek, and the co-founder of Next of Windows.

Last updated: 10/12/2016

Posted in: Tips & Tricks
Discover more: , ,