PowerShell(核心)7+:
在你的会话中执行[cultureinfo]::CurrentUICulture = 'en-US'(见System.Globalization.CultureInfo.CurrentUICulture),让PowerShell从此发出英文消息;
要为您的所有会话预设它,请将该行添加到您的 $PROFILE 文件[1]。
警告:从 v7.0 开始,仅支持英文,因为 PowerShell 尚未本地化 就像 Windows PowerShell 一样 - 在GitHub issue #666 中跟踪进度。
但是,一旦本地化完成,您就可以使用上述方法将会话切换为英语(给定语言),即使您的系统上正在使用不同的 UI 语言(见下文)。
请参阅下文,了解如何更改 Windows 的显示语言作为一个整体,始终保持不变。
Windows PowerShell:
由于可以说是一个错误(因为已在 PowerShell Core 中修复),您不能直接在整个会话中“坚持”更改 UI 语言:
# !! Change of UI culture is effective only for a single command line
PS> [cultureinfo]::CurrentUICulture = 'en-US'; 1 / 0
Attempted to divide by zero.
...
(即使将[cultureinfo]::CurrentUICulture = 'en-US' 添加到您的$PROFILE 文件也无济于事。)
鉴于不再积极开发 Windows PowerShell,因此不太可能修复。
但是,有两种解决方法:
-
任一:将这个不受支持但有效的 hack 放入您的 $PROFILE 文件中[ 1],感谢this answer(精简版):
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
-
注意事项:
-
这是不支持,因为它使用反射来调用非公共类型。话虽如此,鉴于 Windows PowerShell 不再处于积极开发阶段,因此可以安全地假设黑客将继续有效。
-
在 hack 生效后,automatic $PSUICulture variable 确实不反映了有效的 UI 文化,因为它的值是在会话启动时静态确定的。但是,Get-UICulture 确实。
-
该 hack 不在 PowerShell (Core) 7+ 中工作,但您可以简单地将 [cultureinfo]::CurrentUICulture = 'en-US'(如顶部所示)放在您的 $PROFILE 中。
-
或:通过“设置”应用程序或@更改Windows的显示语言作为一个整体 987654326@ cmdlet(Windows 8+ / 服务器 Windows 2012+);这样的更改是持久的并且需要注销或重新启动。
先决条件:无论当前的限制/错误如何,为了从根本上切换到不同的 UI 文化(显示语言),它必须已经安装 作为显示语言,通过设置应用程序 (Settings > Time & Language > Language); 美国英语 (en-US) 不必担心,因为它预装了 Windows。
[1] (current-user, current-host) 配置文件,其完整路径反映在自动$PROFILE 变量中,您的系统上可能尚不存在。
• 要按需创建,请运行if (-not (Test-Path $PROFILE)) { New-Item -Force $PROFILE }
• 例如,要使用 Visual Studio Code 编辑它,请运行code $PROFILE,或者,如果未安装自定义文本编辑器,请使用
notepad $PROFILE
无论哪种方式,如果文件的内容有可能(可能随着时间的推移)包含非 ASCII 字符(例如,é),请务必将文件保存为 UTF-8 with BOM,它适用于 PowerShell (Core) 7+ 和 Windows PowerShell。
有关详细信息,请参阅概念性 about_Profiles 帮助主题。