2 Digit Country Codes generation in Powershell

Christopher Jack 1,611 Reputation points
2021-03-22T16:12:48.303+00:00

Hi,

I am requiring to output a 2 digit country code based on a country.

Array would be something like

$array += [[[[Austria],[AU]],[[Brazil],[BZ]]]]

So i would expect it to be something like

if ($array -match 'Brazil'){ it returns BZ}

What would be the best way of going about that?

Thanks for help

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,388 questions
{count} votes

Accepted answer
  1. Rich Matheisen 45,096 Reputation points
    2021-03-22T19:04:46.553+00:00

    Don't use an array, use a hash instead:

    $hash = @{Austria='AU'; Brazil='BZ'}
    $CountryNames = 'Austria','Brazil','Canada'
    $CountryNames |
        ForEach-Object{
            $CC = $hash[$_]
            Write-Host "Country name '$_' has the code '$CC'"   # doesn't report an error, just an empty country code
        }
    # -- or
    $CountryNames |
        ForEach-Object{
            if($hash.ContainsKey($_)){
                $CC = $hash[$_]
                Write-Host "Country name '$_' has the code '$CC'"
            }
            Else{
                Write-Host "Country name '$_' is not in lookup table"
            }
        }
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Rich Matheisen 45,096 Reputation points
    2021-03-22T19:23:35.05+00:00

    If you'd rather use what data are present in the Windows O/S instead of building/maintaining your own translation table, give this a try. The $hash variable at the end is what you want:

    $AllCultures = [System.Globalization.CultureInfo]::GetCultures([System.Globalization.CultureTypes]::SpecificCultures) # !AllCultures
    
    $AllCultures |
        ForEach-Object{
            $dn = $_.DisplayName.Split("(|)");
            $RegionInfo = New-Object System.Globalization.RegionInfo $PsItem.name;
            [pscustomobject]@{
                    Name = $RegionInfo.Name;
                    EnglishName = $RegionInfo.EnglishName;
                    TwoLetterISORegionName = $RegionInfo.TwoLetterISORegionName;
                    GeoId = $RegionInfo.GeoId;
                    ISOCurrencySymbol = $RegionInfo.ISOCurrencySymbol;
                    CurrencySymbol = $RegionInfo.CurrencySymbol;
                    IsMetric = $RegionInfo.IsMetric;
                    LCID = $PsItem.LCID;
                    Lang = $dn[0].Trim();
                    Country = $dn[1].Trim();
                }
        } |
            # get list of unique country names and ISO codes in Windows
            Select-Object -Unique -prop TwoLetterISORegionName,EnglishName | 
                Sort-Object TwoLetterISORegionName |
                    ForEach-Object{
                        $hash[$_.EnglishName] = $_.TwoLetterISORegionName
                    }
    

    All the information in the PSCustomObject isn't necessary. It doesn't hurt to know it's there, though.

    The code was shamelessly purloined (and slightly modified) from here: powershell-get-list-of-two-letter-country-iso-3166-code-alpha-2-currency-language-and-more

    2 people found this answer helpful.