Working with system locale and user locale

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:

$host.CurrentCulture

Or you can also simply query the $PSculture

$PSCulture
get-item variable:\PSCulture

Or you can use the following .Net object:

[System.Threading.Thread]::CurrentThread.CurrentCulture

You could also query the registry:

(Get-ItemProperty 'HKCU:\Control Panel\International').Locale
(Get-ItemProperty 'HKCU:\Control Panel\International').LocaleName

user locale

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:

(Get-WmiObject Win32_OperatingSystem).locale

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:
http://powershell.com/cs/blogs/ebook/archive/2009/03/08/chapter-6-using-objects.aspx

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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s