powershell : changing the culture of current session

26,714

Solution 1

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.

Solution 2

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)
Share:
26,714
tugberk
Author by

tugberk

Senior Software Engineer and Tech Lead, with a growth mindset belief and 10+ years of practical software engineering experience including technical leadership and distributed systems. I have a passion to create impactful software products, and I care about usability, reliability, observability and scalability of the software systems that I work on, as much as caring about day-to-day effectiveness, productivity and happiness of the team that I work with. I occasionally speak at international conferences (tugberkugurlu.com/speaking), and write technical posts on my blog (tugberkugurlu.com). I currently work at Facebook as a Software Engineer. I used to work at Deliveroo as a Staff Software Engineer in the Consumer division, working on distributed backend systems which have high throughput, low latency and high availability needs. Before that, I used to work at Redgate as a Technical Lead for 4 years, where I led and line-managed a team of 5 Software Engineers. I was responsible for all aspects of the products delivered by the team from technical architecture to product direction. I was also a Microsoft MVP for 7 years between 2012-2019 on Microsoft development technologies.

Updated on May 31, 2020

Comments

  • tugberk
    tugberk almost 4 years

    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?

  • Soeren L. Nielsen
    Soeren L. Nielsen over 7 years
    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.
  • Charlie Joynt
    Charlie Joynt about 7 years
    +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.
  • Tobu
    Tobu over 4 years
    This seems to affect Get-UICulture, yet has no effect on the current session's error messages. Did it make a persistent change instead?