Change your current Powershell session culture, v3.0 +

I want to change the culture of received data and errors in my current Powershell interactive session. I am aware of this powershell question : Changing the culture of the current session and this question Changing the current culture to superuser. The main problem is that it doesn't work with Powershell 3.0 and 4.0.

PS C:\users\me\Documents> [system.threading.thread]::currentthread.currentculture

LCID             Name             DisplayName
----             ----             -----------
1049             ru-RU             ()


PS C:\users\me\Documents> [system.threading.thread]::currentthread.currentculture=[system.globalization.cultureinfo]"en-US"
PS C:\users\me\Documents> [system.threading.thread]::currentthread.currentculture

LCID             Name             DisplayName
----             ----             -----------
1049             ru-RU             ()

      

The UI culture also doesn't accept new customizations. Set-Culture

generally does not work, regardless of whether I use admin access or not - it shouldn't depend on it anyway, since the effect is only valid for one process. Using-Culture

from the MSDN Powershell blog adapted by the SO community works, but only partially, for example with the current culture "ru-RU", I can enter the correct date from the line "6/19/15 2:26:02 PM" which is in culture "en-US" through Using-Culture "en-US" {get-date -date "6/19/15 2:26:02 PM"}

, but getting an error in another language is not possible: say the Using-Culture "en-US" {$null.test='1'}

results in error with the Russian locale as if the culture had not changed.

This behavior has been tested on my local Win7 Professional workstation with Powershell 4.0 installed, and Windows Server 2012 with Powershell 3.0 installed, which is required to parse incorrectly localized date strings. The latter has the "en-US" UI culture and the "ru-RU" system language.

So, is Powershell session culture still possible from PS3 and up, and if so, how? (Or is this a bug again, or a change in PS3 that I'm not aware of?)

+3


source to share


1 answer


The culture change only affects the flow and only applies to this process. Your PS window runs under the current locale, and therefore the thread is in that language. Typing "[System.Threading.Thread] :: CurrentThread.CurrentCulture" into a PS window running under the current system locale will always show that locale.

If you run this in ISE it doesn't explain much:

 function Set-Culture([System.Globalization.CultureInfo] $culture) {
[System.Threading.Thread]::CurrentThread.CurrentUICulture = $culture
[System.Threading.Thread]::CurrentThread.CurrentCulture = $culture }

Set-Culture en-US
[system.threading.thread]::currentthread.currentculture
Pause

      

Or in the PS window:



function Set-Culture([System.Globalization.CultureInfo] $culture) { [System.Threading.Thread]::CurrentThread.CurrentUICulture = $culture ; [System.Threading.Thread]::CurrentThread.CurrentCulture = $culture } ; Set-Culture en-US ; [system.threading.thread]::currentthread.currentculture

      

It works great.

If you want a PS window with a new culture, you need to start it using that culture, rather than trying to change it afterwards.

+2


source







All Articles