Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
849 views
in Technique[技术] by (71.8m points)

windows - Change error message language in PowerShell

I'm trying to display errors in English in powershell so I can search for them more easily online.

At the moment when there's an error it's displayed in french and will look like this :

PS C:UsersOlivierlpthw> type nul > ex2.py
type : Impossible de trouver le chemin d'accès ??C:UsersOlivierlpthw
ul??, car il n'existe pas.
Au caractère Ligne:1 : 1
+ type nul > ex2.py
+ ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:UsersOlivierlpthw
ul:String) [Get-Content], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

PowerShell (Core) 7+:

Execute [cultureinfo]::CurrentUICulture = 'en-US' (see see System.Globalization.CultureInfo.CurrentUICulture) in your session to make PowerShell emit English messages from then on;

To preset it for all your sessions, add the line to your $PROFILE file[1].

Caveat: As of v7.0, only English is supported, because PowerShell has not yet been localized in the way that Windows PowerShell is - progress is being tracked in GitHub issue #666.

However, once localization is complete you'll be able to use the above to switch your sessions to English (a given language), even if a different UI language is in effect on your system (see below).

See below for how to change the display language for Windows as a whole, invariably persistently.


Windows PowerShell:

Due to what is arguably a bug (since fixed in PowerShell Core), you cannot directly make changing the UI language "stick" for an an entire session:

# !! Change of UI culture is effective only for a single command line
PS> [cultureinfo]::CurrentUICulture = 'en-US'; 1 / 0
Attempted to divide by zero.
...

(Even adding [cultureinfo]::CurrentUICulture = 'en-US' to your $PROFILE file doesn't help.)

Given that Windows PowerShell is no longer being actively developed, a fix is unlikely.

However, there are two workarounds:

  • Either: Place this unsupported, but effective hack in your $PROFILE file[1], courtesy of this answer (streamlined):

    function Set-PowerShellUICulture { 
      param([Parameter(Mandatory)] [cultureinfo] $culture) 
    
      [System.Reflection.Assembly]::Load('System.Management.Automation').
        GetType('Microsoft.PowerShell.NativeCultureResolver').GetField('m_uiCulture', 'NonPublic, Static').
          SetValue($null, $culture)
    
    }
    
    # Example call: Set the UI culture to 'en-US' (US English)
    #               Use a value that `[cultureinfo]::new()` understands.
    Set-PowerShellUICulture en-US
    
    • Caveats:
      • This is unsupported, because it uses reflection to call a non-public type. That said, given that Windows PowerShell is no longer under active development, it is safe to assume that the hack will continue to work.

      • With the hack in effect, the automatic $PSUICulture variable does not reflect the effective UI culture, because its value is determined statically at session startup. However, Get-UICulture does.

      • The hack does not work in PowerShell (Core) 7+, but there you can simply place [cultureinfo]::CurrentUICulture = 'en-US', as shown at the top, in your $PROFILE.

  • Or: Change the display language for Windows as a whole, via the Settings application or the Set-WinUILanguageOverride cmdlet (Windows 8+ / Server Windows 2012+); such a change is persistent and requires a logoff or reboot.

    • Caveats:

      • This means that all GUI elements, such as menus, will then be in the chosen language, across all applications (that support localization and the chosen language).

      • Also, the default keyboard layout will switch to the chosen language's.

      • Given that a logoff or reboot is required, this method is also inconvenient for experimenting with different languages.


Prerequisite: Irrespective of current limitations/bugs, in order for switching to a different UI culture (display language) to even be possible fundamentally, it must already be installed as a display language, via the Settings application (Settings > Time & Language > Language); with US English (en-US) that isn't a concern, because it comes preinstalled with Windows.


[1] The (current-user, current-host) profile file, whose full path is reflected in the automatic $PROFILE variable, may not yet exist on your system.
? To create it on demand, run if (-not (Test-Path $PROFILE)) { New-Item -Force $PROFILE }
? To edit it with Visual Studio Code, for instance, run code $PROFILE or, if no custom text editor is installed, use
notepad $PROFILE
Either way, if there's a chance that the file's content will (possibly over time) contain non-ASCII characters (e.g., é), be sure to save the file as UTF-8 with BOM, which works in both PowerShell (Core) 7+ and Windows PowerShell.
For more information, see the conceptual about_Profiles help topic.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

62 comments

56.6k users

...