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.
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 Русский (Россия)
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?)
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:
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 a 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 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With