Find Location and County by IP Address with PowerShell

Locating geolocation details through a web browser is an easy task since we have numerous online tools. However, there are instances where you may want to retrieve such information using PowerShell. This article delves into the method of extracting location, country, and additional geolocation details for a specified IP address using a PowerShell script.

Locating geolocation details requires the use of an external IP geolocation API service like IP-API, which provides free access for non-commercial use and doesn’t require an API key. As far as I’m aware, Windows OS doesn’t have an internal method for extracting geolocation data based on an IP address. Therefore, we need to depend on third-party APIs. While numerous services exist, our suggestion is to opt for IP-API, which ensures high availability even in its free service offering.

Discover Location and ISP Information for a Single IP Address

The following command fetches geolocation details, including country, city, region, zip code, and additionally provides information about the Internet Service Provider (ISP).

$IPAddress = "101.167.212.12"
$LocationInfo = Invoke-RestMethod -Method Get -Uri "http://ip-api.com/json/$IPAddress"
$LocationInfo | FL 

Retrieve Geolocation Information for a List of IP Addresses

The command mentioned above works for a single IP address. If you intend to check the location for a collection of multiple IP addresses, use the following script by inputting the IPs as values within a string array. The script will finally export the output to a CSV file.

$IPAddresses = @('52.108.0.14','101.167.212.12', '101.112.1.12')

$Result = @() #Result array

#Iterate IPs and get location for one by one
ForEach($IPAddress in $IPAddresses)
{
$LocationInfo = Invoke-RestMethod -Method Get -Uri "http://ip-api.com/json/$IPAddress"

$Result += New-Object PSObject -property $([ordered]@{ 
IPAddress = $IPAddress
Status = $LocationInfo.Status
Country = $LocationInfo.country
Region    = $LocationInfo.regionName
City    = $LocationInfo.city
ISP     = $LocationInfo.isp
})

}

#Export the result to a CSV file
$Result | Export-CSV "C:\Temp\IPGeoLocations.CSV" -NoTypeInformation -Encoding UTF8 

Import IP Addresses from a CSV file and Get Location Details

Sometimes, we may need to retrieve a list of IP addresses from a CSV file and obtain geolocation data. Consider the CSV file “IPAddressList.csv” (Download sample CSV), where each row contains an IP address under the column header “IPAddress.” The script provided below imports IPs from the CSV file one by one and retrieves location information, and finally exports the results to another CSV file.

#Read IP Addresses from CSV file
$CSVRecords = Import-CSV "C:\Temp\IPAddressList.csv"
$i = 0;
$TotalRecords = $CSVRecords.Count
 
#Array to add the status result
$Result = @() #Result array
 
#Iterate IP Addresses one by one and get geolocation
Foreach($CSVRecord in $CSVRecords)
{
$IPAddress = $CSVRecord.'IPAddress'

$i++;
Write-Progress -activity "Processing $UserId " -status "$i out of $TotalRecords IPs completed"

$LocationInfo = Invoke-RestMethod -Method Get -Uri "http://ip-api.com/json/$IPAddress"

$Result += New-Object PSObject -property $([ordered]@{ 
IPAddress = $IPAddress
Status = $LocationInfo.Status
Country = $LocationInfo.country
Region    = $LocationInfo.regionName
City    = $LocationInfo.city
ISP     = $LocationInfo.isp
})

}

#Export the result to a CSV file
$Result | Export-CSV "C:\Temp\IPGeoLocations.CSV" -NoTypeInformation -Encoding UTF8
Advertisement

Leave a Comment