Change the current culture of a Powershell session, v3.0+ specific

Question:

I want to change the culture of accepted data and errors in my current interactive Powershell session. I am aware of this question powershell : changing the culture of current session and of this question Changing current culture on SuperUser. The main problem that it doesn’t work with Powershell 3.0 and 4.0.

UI culture also does not accept new settings. Set-Culture in total does not work, regardless of whether I’m using admin access or not – anyway, it shouldn’t be affected by this, as the effect is only for a single process. The Using-Culture from MSDN Powershell blog, adapted by SO community, works, but only partially, for example, with current culture of “ru-RU” I am able to get proper date from “6/19/15 2:26:02 PM” string, which is in the “en-US” culture via Using-Culture "en-US" {get-date -date "6/19/15 2:26:02 PM"}, but receiving an error in another language is not possible: say Using-Culture "en-US" {$null.test='1'} results in an error with Russian locale, as if the culture was not changed.

This behavior was tested on my local Win7 Professional workstation with Powershell 4.0 installed, and a Windows Server 2012 with Powershell 3.0 installed, which is required to parse wrongly localized date strings. The latter has UI culture of “en-US” and system locale “ru-RU”.

So, is changing Powershell session’s culture still possible with PS3 and above, and if yes, how? (Or is it bugs again, or change in PS3 that I am not aware of?)

Answer:

Changing culture only affects a the thread and is only applicable to that process.
Your PS window is launched under the current locale and therefore the thread has that locale. Typing “[System.Threading.Thread]::CurrentThread.CurrentCulture” into a PS window launched under the current system locale, will always show that locale.

If you run this in ISE it should explain it little:

Or, in a PS window:

It works fine.

If you want a PS window with a new culture, you’ll need launch it using that culture, not try and change it afterwards.

Source:

Change the current culture of a Powershell session, v3.0+ specific by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply