11

I am using powershell on windows vista. How do I change the culture of current session? My computer's culture is tr-TR so I am getting the error messages on Turkish. I would like to change to EN?

any chance?

tugberk
  • 57,477
  • 67
  • 243
  • 335
  • 3
    Does the solution to http://stackoverflow.com/questions/2379514/powershell-formatting-values-in-another-culture/ help any? – Gabe Aug 13 '11 at 19:44
  • -look here [thecnet blog](http://social.technet.microsoft.com/Forums/en/winserverpowershell/thread/f4190964-9974-410b-9915-c66966f10edc) – CB. Aug 13 '11 at 20:14

2 Answers2

18

Have a look here: http://blogs.msdn.com/b/powershell/archive/2006/04/25/583235.aspx

and here: http://poshcode.org/2226:

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

Additional Info

To find which values can be used for $culture:

  • This will give you a list of Culture Types:

    [Enum]::GetValues([System.Globalization.CultureTypes])
    
  • Selecting one of the above types (e.g. AllCultures) you can then list the available values of that type:

    [System.Globalization.CultureInfo]::GetCultures( [System.Globalization.CultureTypes]::AllCultures )
    
  • You can then use the Name or Number of the culture you're interested in with the GetCultureInfo method to retrieve the value you're after:

    $culture = [System.Globalization.CultureInfo]::GetCultureInfo(1033)
    $culture = [System.Globalization.CultureInfo]::GetCultureInfo('en-US')
    

NB: Thanks to implicit conversion, you could just pass the culture name or number (i.e. as a string or integer) to the Set-Culture method which would automatically be converted to the expected CultureInfo value.

JohnLBevan
  • 22,735
  • 13
  • 96
  • 178
manojlds
  • 290,304
  • 63
  • 469
  • 417
  • 9
    Ehh... no. This doesn't work. After you switch the culture, read the values out. $host or [System.Threading.Thread]::CurrentThread.CurrentUICulture will not be updated. Tested with PowerShell 5 on win 10 and server 2012. – Soeren L. Nielsen Dec 02 '16 at 10:09
  • 1
    +1 for "this doesn't work"... at least not to change the host culture. However, if you pop a `(Get-Date).ToString()` into the end of the function then you should see the function return a date in the culture specified. – Charlie Joynt Mar 21 '17 at 18:19
4

As the accepted solution by @manojlds actually doesn't work (PS 5.1 on Windows 10) here what works for me (found on github):

$culture = [System.Globalization.CultureInfo]::CreateSpecificCulture("en-US")
$assembly = [System.Reflection.Assembly]::Load("System.Management.Automation")
$type = $assembly.GetType("Microsoft.PowerShell.NativeCultureResolver")
$field = $type.GetField("m_uiCulture", [Reflection.BindingFlags]::NonPublic -bor [Reflection.BindingFlags]::Static)
$field.SetValue($null, $culture)
TNT
  • 3,392
  • 1
  • 24
  • 27
  • This seems to affect Get-UICulture, yet has no effect on the current session's error messages. Did it make a persistent change instead? – Tobu Jan 17 '20 at 09:18