Skip to content

Locate-City

Shows the geographic location of a city

param([string]$city = "")

try {
	if ($city -eq "" ) { $city = Read-Host "Enter the name of the city" }

	Write-Progress "Reading data/worldcities.csv..."
	$table = Import-CSV "$PSScriptRoot/data/worldcities.csv"
	$foundOne = $false
	foreach($row in $table) {
		if ($row.city -eq $city) {
			$foundOne = $true
			$country = $row.country
			$region = $row.admin_name
			$lat = $row.lat
			$lng = $row.lng
			if ($lat -lt 0.0) { $latText = "$(-$lat)?S" } else { $latText = "$lat?N" }
			if ($lng -lt 0.0) { $lngText = "$(-$lng)?E" } else { $lngText = "$lng?W" }
			$population = $row.population
			"??$city in $region ($country) with population $population is at $latText, $lngText."
		}
	}
	if (-not $foundOne) { throw "There's no city '$city' in the database" }
	exit 0 # success
} catch {
	"?? ERROR: $($Error[0]) (script line $($_.InvocationInfo.ScriptLineNumber))"
	exit 1
}

Specifies the name of the city (ask the user if not given)

An interactive directory of PowerShell scripts.