
一、遇到的问题
今天终于把 Codex 在 Windows 下的中文乱码问题彻底解决了,记录一下整个过程。
先说现象:Codex 在 Windows 下执行命令时,所有中文输出全是乱码——方块字、问号、稀奇古怪的符号,完全无法阅读。比如让它列个带中文文件名的目录,返回的东西根本没法看;让它输出一段中文提示信息,终端里显示的是一坨天书。
一开始以为是 Codex 本身的 bug,折腾了很久才发现问题出在 Windows 的终端编码体系上。
二、乱码根因分析
2.1 核心矛盾
问题不在 Codex,而在 Windows 终端默认的编码机制与 Codex 使用的编码不一致:
- Codex(基于 Node.js) 默认使用 UTF-8 编码输出。
- Windows 默认终端(CMD / PowerShell 5.1) 使用的是 历史代码页(中文系统常见 936 / GBK)。
这个差异导致了一条"烂链路":
Codex(UTF-8)→ Windows Console(GBK / 936)→ VS Code 终端 → 中文乱码
两边编码不一致,UTF-8 的多字节中文字符被按 GBK 单字节/双字节方式解析,结果自然是一团乱码。
2.2 为什么 Linux / macOS 没这个问题?
Linux 和 macOS 全链路默认 UTF-8:
locale = UTF-8
终端 = UTF-8
Shell = UTF-8
CLI = UTF-8
无需任何额外配置,全链路天然一致。而 Windows 的 CLI 层一直背负着历史包袱——GUI 层用 Unicode,终端层用历史代码页,两者割裂至今。
2.3 CMD / PowerShell 5.1 / PowerShell 7 的真实差异
| 工具 | 定位 | 编码 |
|---|---|---|
| cmd.exe | DOS 时代遗产,兼容层 | 强依赖代码页(默认 GBK) |
| PowerShell 5.1 | 系统管理遗产,基于 .NET Framework | 继承旧控制台编码 |
| PowerShell 7(pwsh) | 现代开发者终端,基于 .NET(跨平台) | 可控 UTF-8 |
结论很清晰:需要切换到 PowerShell 7(pwsh),并显式声明 UTF-8 编码。
三、解决方案
Step 1:下载并安装 MSI 版 PowerShell 7
重要:不要用 Microsoft Store 版(Appx),Codex 沙箱环境中 Store 版可能触发权限问题(
CreateProcessAsUserW failed: 5)。必须使用 MSI 安装版。
下载链接(x64):
https://github.com/PowerShell/PowerShell/releases/download/v7.6.2/PowerShell-7.6.2-win-x64.msi
双击运行,一路默认安装即可。安装时会自动将 C:\Program Files\PowerShell\7 加入 PATH。
验证安装:
pwsh --version
预期输出类似:
PowerShell 7.6.2
Step 2:卸载 Store / Appx 版 PowerShell
如果之前安装过 Store 版 PowerShell 7,需要卸载掉,避免路径冲突。
先关闭所有占用 PowerShell 进程的软件(Codex、VS Code、IDEA 等),然后执行:
# 确认是否存在 Store 版
Get-AppxPackage Microsoft.PowerShell
如果有输出,说明存在 Store 版,执行卸载:
Get-AppxPackage Microsoft.PowerShell | Remove-AppxPackage
再次确认已卸载:
Get-AppxPackage Microsoft.PowerShell
预期输出: 无任何输出(表示已卸载干净)。
Step 3:调整 PATH 顺序
确保 MSI 版 pwsh 的路径在 PATH 中排在 WindowsApps 前面。打开系统环境变量,在用户 PATH 中确认以下顺序:
C:\Program Files\PowerShell\7 ← MSI 版(排前面)
C:\Users\<用户名>\AppData\Local\Microsoft\WindowsApps ← WindowsApps(排后面)
这样执行 pwsh 时会优先使用 MSI 版。
验证解析顺序:
Get-Command pwsh -All
预期输出: 第一项应为:
C:\Program Files\PowerShell\7\pwsh.exe
Step 4:关闭 App Execution Alias
如果 Get-Command pwsh -All 的结果中仍然出现 C:\Users\<用户名>\AppData\Local\Microsoft\WindowsApps\pwsh.exe,说明 App Execution Alias 残留。需要手动关闭:
- 打开 Windows 设置。
- 进入 应用 → 高级应用设置 → 应用执行别名。
- 找到 PowerShell / pwsh.exe 相关别名,全部关闭。
关闭后再执行 Get-Command pwsh -All,确认只剩 MSI 版路径。
Step 5:为 PowerShell 7 配置 UTF-8 Profile
这是解决乱码的核心步骤。启动 pwsh 并配置 profile:
# 进入 pwsh
pwsh
# 确认版本为 Core(非 Windows PowerShell 5.1)
$PSVersionTable.PSEdition
预期输出:
Core
然后创建并编辑 profile:
# 创建 profile(如果不存在)
New-Item -ItemType File -Path $PROFILE -Force
# 编辑 profile
notepad $PROFILE
在打开的记事本中写入以下内容:
[Console]::InputEncoding = [System.Text.UTF8Encoding]::new()
[Console]::OutputEncoding = [System.Text.UTF8Encoding]::new()
$OutputEncoding = [System.Text.UTF8Encoding]::new()
保存并关闭记事本。
Step 6:重启终端并验证
关闭当前终端,重新打开 pwsh,验证 UTF-8 配置已生效:
[Console]::OutputEncoding.CodePage
预期输出:
65001
65001 即 UTF-8 的代码页编号。如果输出 936(GBK),说明 profile 未生效,检查 $PROFILE 文件内容是否正确。
Step 7:Codex 侧最终验证
- 完全退出 Codex。
- 在任务管理器中确认没有残留的 Codex 进程。
- 重新打开 Codex。
- 新建一个对话,让 Codex 执行:
$PSVersionTable.PSVersion
预期结果: Codex 确认框中显示的路径为 C:\Program Files\PowerShell\7\pwsh.exe,且中文输出正常可读。
四、总结
这次折腾下来,核心就一句话:Windows CLI 不会自动 UTF-8,必须显式声明。 换成 MSI 版 PowerShell 7 + 配置 UTF-8 Profile 之后,Codex 的中文输出终于正常了,整个开发体验直线上升。
如果你也在 Windows 下用 Codex 遇到乱码,按上述步骤走一遍,基本都能解决。
参考来源
- Windows 下 Codex 中文乱码问题解决方案 - 腾讯云开发者社区(乱码根因分析与 UTF-8 配置方案)
- Windows下CodexApp修复和优化PowerShell命令执行效率和权限的方法 - 小萝卜头论坛(MSI 版安装、Store 版卸载、PATH 调整与 Alias 关闭)
- All rights reserved.
- No part of this website, including text and images, may be reproduced, modified, distributed, or transmitted in any form or by any means, without the prior written permission of the author.
- Unauthorized commercial use is strictly prohibited.
- Unauthorized personal use is strictly prohibited.
