Skip to content

locate-zip-code

Prints the geo location of a zip-code

#Requires -Version 5.1

param([string]$CountryCode = "", [string]$ZipCode = "")

try {
	if ($CountryCode -eq "" ) { $CountryCode = read-host "Enter the country code" }
	if ($ZipCode -eq "" ) { $ZipCode = read-host "Enter the zip code" }

	write-progress "Reading data/zip-codes.csv..."
	$Table = import-csv "$PSScriptRoot/data/zip-codes.csv"

	$FoundOne = 0
	foreach($Row in $Table) {
		if ($Row.country -eq $CountryCode) {
			if ($Row.postal_code -eq $ZipCode) {
				$Country=$Row.country
				$City = $Row.city
				$Lat = $Row.latitude
				$Lon = $Row.longitude
				write-output "* $Country $ZipCode $City is at $Lat°N, $Lon°W"
				$FoundOne = 1
			}
		}
	}

	if ($FoundOne) {
		exit 0
	}
	throw "Zip-code $ZipCode in country $CountryCode not found"
} catch {
throw
}

Specifies the country code

Specifies the zip code

An interactive directory of PowerShell scripts.