Lazy Diary @ Hatena Blog

PowerShell / Java / miscellaneous things about software development, Tips & Gochas. CC BY-SA 4.0/Apache License 2.0

Change culture (locale) of current PowerShell process

You can change culture (locale) into English with chcp 437 in Windows 7.

In contrast, You cannot change culture with chcp in Windows 10.

In Windows 10, if you want to execute single PowerShell script in another culture, you can execute the script like this:

[Threading.Thread]::CurrentThread.CurrentUICulture = 'en-US'; .\myscript.ps1

If you want to change culture throughout every command in a PowerShell process, you have to change the culture setting cached in PowerShell runtime with reflection like this:

# example: Set-PowerShellUICulture -Name "en-US"

function Set-PowerShellUICulture {
    param([Parameter(Mandatory=$true)]
          [string]$Name)

    process {
        $culture = [System.Globalization.CultureInfo]::CreateSpecificCulture($Name)
       
        $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)
    }
}

Note: Some cmdlets like Add-Type are not affected this setting.

(from https://gist.github.com/sunnyone/7486486 via https://twitter.com/altrive/status/401349992536756224?s=20)