4

We're trying to convert the CountryCode in to a human readable String.

Code:

$OS = Get-CimInstance -ClassName Win32_OperatingSystem

$OS | Select-Object CountryCode, OSLanguage, 
    @{N = 'OSDefaultLanguage'; E = {New-Object System.Globalization.CultureInfo([Int]$_.OSLanguage)}},
    @{N = 'OSCountryCode'; E = {New-Object System.Globalization.CultureInfo([Int]$_.CountryCode)}}

In the example above the property OSCountryCode is what we need. But it returns the value ar which is Argentina but it should return United States according to the documentation for value 1.

How can value 1 be correctly converted to US or something similar?

DarkLite1
  • 13,637
  • 40
  • 117
  • 214
  • 2
    `CountryCode` is the _country calling code_ (the phone number routing prefix) for the associated country. You'll need to query an external API to convert the calling code to a country name – Mathias R. Jessen Oct 31 '17 at 10:54
  • 1
    Or just don't use WMI and retrieve `[Globalization.RegionInfo]::CurrentRegion.TwoLetterISORegionName` (you can do this with PowerShell remoting for other machines). – Jeroen Mostert Oct 31 '17 at 10:57
  • When I run the code suggested by @JeroenMostert with `Invoke-Command` on another machine it always returns my own country and not that of the remote machine (Ex. 1 = US, 32 = BE). Is there a way to convert the value from the class `Win32_OperatingSystem`? – DarkLite1 Oct 31 '17 at 12:56

1 Answers1

1
$OS = Get-CimInstance -ClassName Win32_OperatingSystem

$Culture = [System.Globalization.CultureInfo]::GetCultures("SpecificCultures") | Where {$_.LCID -eq $OS.OSLanguage}
$RegionInfo = New-Object System.Globalization.RegionInfo $Culture.Name

$OS | Select-Object CountryCode, OSLanguage, 
    @{N = 'OSDefaultLanguage'; E = {New-Object System.Globalization.CultureInfo([Int]$_.OSLanguage)}},
    @{N = 'OSCountryCode'; E = {$RegionInfo.TwoLetterISORegionName}},
    @{N = 'OSCountryName'; E = {$RegionInfo.DisplayName}}
iRon
  • 20,463
  • 10
  • 53
  • 79
  • Thx @iRon but that's not really what was asked. We're looking to match the `CountryCode` an `[int]` with the `CountryName` a `[String]` – DarkLite1 Oct 31 '17 at 13:51
  • @DarkLite1, sorry, I missed that, I have change my answer accordingly (and also added the country name. – iRon Oct 31 '17 at 14:51
  • Sorry for the confusion @iRon but it's not converting the `$OS.CountryCode` to the two letter country name. The `$OS.CountryCode` is different from the `$OS.OSLanguage`. A given system can have OS language x and country Y. – DarkLite1 Nov 03 '17 at 09:06
  • This means that you will get multiple results if you want to convert a language to a country (e.g.dutch is spoken in both the Netherlands and Belgium) and also visa versa (e Belgium they speak French and – iRon Nov 03 '17 at 11:32
  • I think you're also getting a bit confused, just like I was. The `CountryCode` is 1 and the `OSLangauge` is 1033. The `CountryCode` 1 needs to be converted to `US` by only using the `CountryCode` and not the `OSLanguage` because it's not related. You can have a machine in `Germany` with language `Polish` without a problem.... – DarkLite1 Nov 03 '17 at 12:34
  • 1
    I do not have the resource to confirm this, but I think you will need to run `Get-CimInstance -ClassName Win32_OperatingSystem` under the SYSTEM account to get the system's OSLanguage/LCID ([Locale ID](https://msdn.microsoft.com/en-us/library/ms912047(v=winembedded.10).aspx)). If that doesn't work out for you, then you probably have to build a hard coded reference or get your reference from external, see: https://stackoverflow.com/a/25677570/1701026 – iRon Nov 04 '17 at 10:30