解决 PowerShell 中文乱码问题
问题现象
在使用 PowerShell 时,经常遇到中文显示乱码的问题:
UTF-8 缂栫爜宸查厤缃畬鎴愶紒
这不仅影响阅读体验,还会导致中文文件名、日志输出等问题。
根本原因
PowerShell 中文乱码的根本原因是编码不一致:
- 控制台代码页: 默认使用 GBK (代码页 936),而脚本文件使用 UTF-8
- 配置文件编码: PowerShell Profile 的编码与控制台编码不匹配
- 加载时机问题: 配置文件在编码设置之前就已经加载了
完整解决方案
方案一:修改 PowerShell Profile (适用于普通 PowerShell)
1. 创建或编辑 PowerShell Profile
首先检查 Profile 是否存在:
powershell
Test-Path $PROFILE
如果不存在,创建它:
powershell
New-Item -Path $PROFILE -ItemType File -Force
2. 编辑 Profile 文件
使用记事本打开:
powershell
notepad $PROFILE
⚠️ 重要 : 文件必须以 UTF-8 BOM 格式保存!
添加以下内容:
powershell
# PowerShell UTF-8 Encoding Configuration
# =============================================
# Set console encoding to UTF-8
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
[Console]::InputEncoding = [System.Text.Encoding]::UTF8
$OutputEncoding = [System.Text.Encoding]::UTF8
# Set code page to UTF-8
chcp 65001 | Out-Null
# Set default parameter encoding
$PSDefaultParameterValues['Out-File:Encoding'] = 'utf8'
$PSDefaultParameterValues['*:Encoding'] = 'utf8'
3. 保存为 UTF-8 BOM 格式
在记事本中保存时:
- 点击 "另存为"
- 在编码下拉框中选择 "UTF-8"
- 保存文件
方案二:修改 Windows Terminal 配置 (推荐)
如果你使用 Windows Terminal,这是最优雅的解决方案。
1. 找到配置文件
配置文件位置:
%LocalAppData%\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\settings.json
或直接在 Windows Terminal 中按 Ctrl + , 打开设置。
2. 修改 PowerShell 配置
找到 "profiles" -> "list" 中的 PowerShell 配置,修改 "commandline":
json
{
"commandline": "%SystemRoot%\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -NoExit -Command \"chcp 65001 | Out-Null; [Console]::OutputEncoding = [System.Text.Encoding]::UTF8; [Console]::InputEncoding = [System.Text.Encoding]::UTF8; $OutputEncoding = [System.Text.Encoding]::UTF8\"",
"guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
"hidden": false,
"name": "Windows PowerShell"
}
3. 修改 Anaconda PowerShell 配置 (如果使用)
json
{
"commandline": "%WINDIR%\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -NoExit -Command \"chcp 65001 | Out-Null; [Console]::OutputEncoding = [System.Text.Encoding]::UTF8; [Console]::InputEncoding = [System.Text.Encoding]::UTF8; $OutputEncoding = [System.Text.Encoding]::UTF8; & 'C:\\path\\to\\anaconda3\\shell\\condabin\\conda-hook.ps1' ; conda activate 'C:\\path\\to\\anaconda3' \"",
"name": "Anaconda PowerShell Prompt (anaconda3)",
"icon": "C:\\path\\to\\anaconda3\\Menu\\anaconda_powershell_prompt.ico",
"startingDirectory": "C:\\Users\\YourUsername"
}
4. 设置中文字体 (推荐)
在 "profiles" -> "defaults" 中添加:
json
"profiles": {
"defaults": {
"font": {
"face": "Microsoft YaHei UI"
}
},
"list": [
// ...
]
}
完整配置示例:
json
{
"$help": "https://aka.ms/terminal-documentation",
"$schema": "https://aka.ms/terminal-profiles-schema",
"profiles": {
"defaults": {
"font": {
"face": "Microsoft YaHei UI"
}
},
"list": [
{
"commandline": "%SystemRoot%\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -NoExit -Command \"chcp 65001 | Out-Null; [Console]::OutputEncoding = [System.Text.Encoding]::UTF8; [Console]::InputEncoding = [System.Text.Encoding]::UTF8; $OutputEncoding = [System.Text.Encoding]::UTF8\"",
"guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
"hidden": false,
"name": "Windows PowerShell"
}
]
}
}
方案三:通过注册表设置 (全局生效)
以管理员身份运行 PowerShell:
powershell
# 设置当前用户的控制台代码页
New-Item -Path 'HKCU:\Console' -Force | Out-Null
Set-ItemProperty -Path 'HKCU:\Console' -Name 'CodePage' -Value 65001 -Type DWord
验证修复
重新打开 PowerShell,运行测试命令:
powershell
Write-Host "测试中文显示: 你好世界! 🎉"
应该看到正确的输出:
测试中文显示: 你好世界! 🎉
常见问题
Q1: 为什么修改 Profile 后还是乱码?
A: Profile 文件本身的编码必须是 UTF-8 BOM 格式。如果使用普通文本编辑器,可能保存为 ANSI 或其他编码。
解决方法:
- 使用 PowerShell 命令创建 UTF-8 BOM 文件:
powershell
$content = @'
# Your content here
'@
$utf8WithBom = New-Object System.Text.UTF8Encoding $True
[System.IO.File]::WriteAllText($PROFILE, $content, $utf8WithBom)
- 或使用支持 UTF-8 BOM 的编辑器 (如 VS Code) 保存。
Q2: Windows Terminal 配置修改后不生效?
A: 确保 JSON 格式正确,没有语法错误。
解决方法:
- 使用 JSON 验证工具检查格式
- 完全关闭 Windows Terminal 后重新打开
Q3: 每次打开都要手动运行 chcp 65001?
A: 这说明配置没有正确加载。
解决方法:
- 检查 Profile 文件路径:
echo $PROFILE - 确认 Profile 存在:
Test-Path $PROFILE - 如果使用 Windows Terminal,使用方案二
技术原理
代码页 (Code Page)
- 936: GBK 编码 (简体中文默认)
- 65001: UTF-8 编码 (国际标准)
PowerShell 5.x 及以下版本默认使用系统代码页,通常中文 Windows 是 GBK。
BOM (Byte Order Mark)
UTF-8 BOM 是文件开头的三个字节 EF BB BF,用于标识文件编码。
PowerShell 需要 BOM 来正确识别 Profile 文件的编码,否则会使用系统默认编码 (GBK) 读取,导致中文乱码。
编码优先级
Windows Terminal commandline > PowerShell Profile > 注册表设置 > 系统默认
这就是为什么在 Windows Terminal 中设置最有效。
最佳实践
- 使用 Windows Terminal: 更好的渲染、配置灵活
- 统一使用 UTF-8: 脚本文件、配置文件全部使用 UTF-8 BOM
- 设置中文字体: Microsoft YaHei UI 或 Cascadia Code
- 版本控制: PowerShell Core (6+) 默认使用 UTF-8,问题较少
相关资源
总结
PowerShell 中文乱码是编码不一致导致的。通过以下三步可以彻底解决:
- ✅ 设置控制台代码页为 UTF-8 (65001)
- ✅ 配置 PowerShell 使用 UTF-8 编码
- ✅ 确保配置文件使用 UTF-8 BOM 格式保存
推荐方案 : 使用 Windows Terminal + 修改 settings.json,一次配置,永久生效。
发布日期 : 2025-01-05
适用版本 : Windows PowerShell 5.x, PowerShell Core 6+, Windows Terminal
测试环境: Windows 10/11