Last week while working with the WDRAP tool (Risk and Health Assessment Program for Windows Desktop) from Microsoft, it complained about the system locale not being en-US (English United States).
So I started digging into this subject and found that there are many ways to get the user locale.
You can query the $host variable in your powrshell session:
Or you can also simply query the $PSculture
$PSCulture get-item variable:\PSCulture
Or you can use the following .Net object:
You could also query the registry:
(Get-ItemProperty 'HKCU:\Control Panel\International').Locale (Get-ItemProperty 'HKCU:\Control Panel\International').LocaleName
But, the user locale isn’t what I was looking for. The only way I found to query the System locale is by using WMI:
The WMI query returns a system string that is actually the hexadecimal number of the system locale. I was close to the result and finally used the System.Globalization.CultureInfo .Net object preloaded in the session.
The tip of the week page about Formatting Numbers and Dates Using the CultureInfo Object has a link to the .net object system.globalization.cultureinfo.
Here is how to use it
[System.Globalization.CultureInfo]("en-US") # The object accepts also a decimal number or a decimal number written in hexadecimal: [System.Globalization.CultureInfo](1033) [System.Globalization.CultureInfo](0x409)
Now, I’ve just got to convert the system.string to either a decimal number or a decimal number written in hexadecimal like this:
# Using decimal number written in hexadecimal [System.Globalization.CultureInfo]([int]("0x" + (Get-WmiObject Win32_OperatingSystem).locale)) # Using a decimal number, i.e, a system.int32 [System.Globalization.CultureInfo]([Convert]::ToInt32((Get-WmiObject Win32_OperatingSystem).locale,16))
Here are additional pages that could be as well useful resources:
This great page speaks also about the system.globalization.cultureinfo and much more:
This one shows how to Convert decimal to binary to hexadecimal and vice versa
This page explains precisely what Locale Names are.
And finally this page shows the full list of hexadecimal values and their Locale ID displayname: http://msdn.microsoft.com/en-us/library/cc233968.aspx