#PSTip Get a list of geographical locations

Note: This tip requires PowerShell 3.0 or above.

In a previous tip I wrote about the International module. One of the commands of the module, Get-WinHomeLocation, returns a GeoID object which represents the home location (Country and Region) of the current user account.

PS> Get-WinHomeLocation

GeoId HomeLocation
----- ------------
244   United States

We can use the Set-WinHomeLocation to change the location but it requires us to have the GeoID numeric value in advance:

Set-WinHomeLocation -GeoId <int32>

Unfortunately, none of the International module commands gets you a list of countries and their GeoId value. Luckily, there’s a .NET class, RegionaInfo, you can use to get the GeoId of a specific culture. The following function creates a list of RegionalInfo objects for all InstalledWin32Cultures cultures on the current system. Run it without any parameters and you’ll get the complete list, or supply a (partial) value.

function Get-RegionInfo($Name='*')
    $cultures = [System.Globalization.CultureInfo]::GetCultures('InstalledWin32Cultures')

    foreach($culture in $cultures)
           $region = [System.Globalization.RegionInfo]$culture.Name

           if($region.DisplayName -like $Name)
       catch {}

PS> Get-RegionInfo -Name *isr*

Name                         : he-IL
EnglishName                  : Israel
DisplayName                  : Israel
NativeName                   : ישראל
TwoLetterISORegionName       : IL
ThreeLetterISORegionName     : ISR
ThreeLetterWindowsRegionName : ISR
IsMetric                     : True
GeoId                        : 117
CurrencyEnglishName          : Israeli New Shekel
CurrencyNativeName           : שקל חדש
CurrencySymbol               : ₪
ISOCurrencySymbol            : ILS

PS> Get-RegionInfo | Sort-Object DisplayName | Select-Object Name,DisplayName,GeoId | Out-GridView


Filed in: Columns, Tips and Tricks Tags: , , , ,
© 6818 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: