AI编程告别控制台中文乱码:升级 PowerShell 7 并配置 UTF-8

参考·5 分钟·PowerShell 7 迁移指南:UTF-8 配置 中文乱码彻底修复

升级到 PowerShell 7 并配置 UTF-8:彻底解决 Windows 终端中文乱码

适用场景:在 Windows 环境下使用 Codex、VS Code、opencode、Kilo Code、Node.js、pnpm、Git 等现代开发工具时,终端出现中文乱码、路径乱码或报错信息不可读的情况。


1. 为什么会出现乱码?

Windows 系统内置的是 Windows PowerShell 5.1,其可执行文件路径为:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

该版本在中文编码、UTF-8 输出及现代 CLI 工具兼容性上存在明显缺陷,常见乱码现象如下:

▒▒▒▒▒▒▒ļ▒▒▒δ▒ҵ▒
中文路径乱码
npm / pnpm / git 报错乱码
Codex 读取工具输出后判断错误
VS Code 终端显示异常

解决方案:升级到 PowerShell 7,其路径通常为:

C:\Program Files\PowerShell\7\pwsh.exe

两个版本的启动命令对比:

版本 命令
PowerShell 7 pwsh
Windows PowerShell 5.1 powershell

2. 下载 PowerShell 7

前往 PowerShell 官方 Release 页面:

https://github.com/PowerShell/PowerShell/releases

下载 Windows 64 位 MSI 安装包,文件名格式如下:

PowerShell-7.x.x-win-x64.msi

例如:

PowerShell-7.6.3-win-x64.msi

版本号以你实际下载的最新版为准。


3. 安装 PowerShell 7

双击 MSI 安装包,建议右键选择:

以管理员身份运行

安装目录建议保持默认:

C:\Program Files\PowerShell\

安装完成后,可执行文件路径为:

C:\Program Files\PowerShell\7\pwsh.exe

推荐勾选的安装选项:

✅ Add PowerShell to Path Environment Variable
✅ Register Windows Event Logging Manifest
✅ Enable PowerShell remoting
⬜ Disable Telemetry
✅ Add 'Open here' context menus to Explorer
✅ Add 'Run with PowerShell 7' context menu for PowerShell files

其中最关键的是:

Add PowerShell to Path Environment Variable

勾选后才能在任意终端中直接执行:

pwsh

4. 验证 PowerShell 7 是否安装成功

打开任意 PowerShell 或 CMD 窗口,执行:

pwsh -v

正常输出类似:

PowerShell 7.6.3

再执行以下命令确认路径:

where.exe pwsh

应输出:

C:\Program Files\PowerShell\7\pwsh.exe

注意:若在旧版 PowerShell 窗口中执行 $PSVersionTable.PSVersion,仍会看到 5.1,这是正常的——因为当前窗口本身就是旧版。需先执行 pwsh 进入 PowerShell 7,再验证版本。

在 PowerShell 7 中执行:

$PSVersionTable.PSVersion

正常应显示:

Major  Minor  Patch
7      6      3

5. 配置 PowerShell 7 使用 UTF-8

进入 PowerShell 7 后,打开配置文件:

notepad $PROFILE

若提示文件不存在,选择创建即可。

在文件末尾加入以下三行:

[Console]::InputEncoding = [System.Text.UTF8Encoding]::new()
[Console]::OutputEncoding = [System.Text.UTF8Encoding]::new()
$OutputEncoding = [System.Text.UTF8Encoding]::new()

保存后,关闭所有 PowerShell 窗口,重新打开 PowerShell 7。

验证编码是否生效:

[Console]::InputEncoding
[Console]::OutputEncoding
$OutputEncoding

也可以执行:

chcp

若显示:

活动代码页: 65001

说明当前控制台已切换为 UTF-8 编码。


6. Windows Terminal 设置默认使用 PowerShell 7

打开 Windows Terminal,进入:

设置 → 启动 → 默认配置文件

选择 PowerShell,注意不要选 Windows PowerShell

两者区别如下:

PowerShell          = PowerShell 7.x,路径 C:\Program Files\PowerShell\7\pwsh.exe
Windows PowerShell  = 旧版 5.1,路径 C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

7. VS Code 设置默认终端为 PowerShell 7

7.1 图形界面方式

在 VS Code 中按 Ctrl + Shift + P,输入:

Terminal: Select Default Profile

选择 PowerShell。若列表中出现两个 PowerShell,请选择路径为:

C:\Program Files\PowerShell\7\pwsh.exe

而非:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

关闭当前终端,重新新建终端后执行以下命令验证:

$PSVersionTable.PSVersion

应显示 7.x

7.2 手动配置(VS Code 未自动识别时)

Ctrl + Shift + P,输入:

Preferences: Open User Settings (JSON)

加入或合并以下配置:

{
  "terminal.integrated.profiles.windows": {
    "PowerShell 7": {
      "path": "C:\\Program Files\\PowerShell\\7\\pwsh.exe",
      "args": ["-NoLogo"],
      "icon": "terminal-powershell"
    },
    "Git Bash": {
      "path": "C:\\Program Files\\Git\\bin\\bash.exe",
      "args": ["--login", "-i"]
    }
  },
  "terminal.integrated.defaultProfile.windows": "PowerShell 7"
}

保存后关闭旧终端,重新新建终端,验证:

$PSVersionTable.PSVersion
where.exe pwsh

期望结果:

PowerShell 版本:7.x
pwsh 路径:C:\Program Files\PowerShell\7\pwsh.exe

8. 在 Codex 中确认执行环境

让 Codex Agent 执行以下命令,确认其运行环境:

$PSVersionTable.PSVersion
[Console]::InputEncoding
[Console]::OutputEncoding
$OutputEncoding
chcp

理想结果:

PowerShell 版本:7.x
InputEncoding:utf-8
OutputEncoding:utf-8
chcp:65001

若 Codex 返回的是:

PowerShell 5.1
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

说明 Codex 仍在使用旧版 Windows PowerShell,中文乱码问题可能持续存在。


9. 乱码问题状态判断

✅ 已基本解决

✅ Codex 使用 PowerShell 7
✅ VS Code 默认终端是 PowerShell 7
✅ PowerShell 7 配置了 UTF-8
✅ chcp 显示 65001
✅ npm / pnpm / git 中文输出正常

❌ 仍可能乱码

❌ Codex 仍使用 Windows PowerShell 5.1
❌ VS Code 终端仍是 C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
❌ 某些第三方工具本身不是 UTF-8 输出
❌ 项目脚本强制使用 GBK / ANSI
❌ 终端字体不支持中文

10. 为 opencode 配置 PowerShell 7

若使用 opencode,可在 opencode.json 中指定 PowerShell 7:

{
  "$schema": "https://opencode.ai/config.json",
  "shell": {
    "path": "C:\\Program Files\\PowerShell\\7\\pwsh.exe",
    "args": ["-NoLogo", "-NoProfile"]
  }
}

若更习惯 Git Bash,可改为:

{
  "$schema": "https://opencode.ai/config.json",
  "shell": {
    "path": "C:\\Program Files\\Git\\bin\\bash.exe",
    "args": ["-lc"]
  }
}

11. 常用验证命令速查

查看当前 PowerShell 版本

$PSVersionTable.PSVersion

查看 pwsh 安装路径

where.exe pwsh

查看编码设置

[Console]::InputEncoding
[Console]::OutputEncoding
$OutputEncoding
chcp

测试中文输出

Write-Output "中文测试:你好,Codex / VS Code / PowerShell 7"

测试 Git 中文输出

git status

测试 Node / pnpm 版本输出

node -v
npm -v
pnpm -v

12. 推荐的最终环境配置

Windows Terminal 默认终端:PowerShell 7
VS Code 默认终端:PowerShell 7
Codex 执行环境:PowerShell 7 + UTF-8
opencode shell:PowerShell 7 或 Git Bash
旧版 Windows PowerShell 5.1:不作为开发主力终端

配置目标:

减少中文乱码
减少 Codex 误判工具输出
减少重复试错
减少 token 浪费
提高 Windows 本地开发稳定性

13. 快速排查清单

检查项 命令 正确结果
PowerShell 7 是否安装 pwsh -v PowerShell 7.x
当前终端是否是 PowerShell 7 $PSVersionTable.PSVersion Major = 7
pwsh 路径是否正确 where.exe pwsh C:\Program Files\PowerShell\7\pwsh.exe
控制台代码页 chcp 65001
输入编码 [Console]::InputEncoding utf-8
输出编码 [Console]::OutputEncoding utf-8
VS Code 默认终端 Terminal: Select Default Profile PowerShell 7
Codex 实际执行环境 让 Codex 执行 $PSVersionTable.PSVersion 7.x

14. 总结

在 Windows 下使用 Codex / VS Code 进行 AI 辅助编程时,强烈建议将默认终端从 Windows PowerShell 5.1 切换到 PowerShell 7,并在 $PROFILE 中显式配置 UTF-8 编码。

这一改动可以大幅减少中文乱码、路径乱码和命令输出乱码问题,同时避免因乱码导致 Codex 反复试错和不必要的 token 消耗,显著提升 Windows 本地开发体验。