5

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?)

Community
  • 1
  • 1
Vesper
  • 18,599
  • 6
  • 39
  • 61

1 Answers1

2

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.

Scepticalist
  • 3,737
  • 1
  • 13
  • 30
  • To start a PSwindow under the culture, just add the following to either of the above: **start-process 'c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe'** – Scepticalist Apr 14 '16 at 12:57
  • I've tried this with no luck(keeps showing non english output), what i'm doing wrong? `Set-Culture en-US start-process 'c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe'` and inside i run `netsh wlan show drivers` – FrakyDale Jul 31 '17 at 12:18
  • You can't do that. May I should clarify further, set culture must be done in the current process - either run the command in your new process window as part of the script or you can do something like Start-Job -scriptblock {function Set-Culture([System.Globalization.CultureInfo] $culture) { [System.Threading.Thread]::CurrentThread.CurrentUICulture = $culture ; [System.Threading.Thread]::CurrentThread.CurrentCulture = $culture } ; Set-Culture en-US ; etc.....} – Scepticalist Dec 03 '18 at 12:38