Windows+codex开发必看:Codex如何默认使用PowerShell 7解决默认调用 PowerShell 5 导致中文、引号频繁出错的问题
前言最近在 Windows 上使用 Codex 做本地开发时经常遇到一些很烦的问题中文路径或中文输出异常命令里有引号、JSON、正则时容易解析失败一些 PowerShell 7 支持的语法在 PowerShell 5.1 里不可用明明命令本身没问题但 Codex 调用时表现不稳定导致codex任务效率很低且白白浪费token。排查后发现Codex 本地 shell 命令默认进入的是系统自带的 Windows PowerShell 5.1Get-Process-Id$PID|Select-ObjectId,ProcessName,Path$PSVersionTable.PSVersion输出类似ProcessName: powershell Path: C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe PSVersion: 5.1.x解决思路很直接安装 PowerShell 7并让 Codex 后续默认能找到pwsh.exe。下载安装 PowerShell 7官方推荐方式可以使用 WinGet 或 MSI 安装包。Microsoft 官方文档提供了 Windows 安装说明也列出了 MSI、MSIX、ZIP 等安装方式。WinGet 安装winget install--id Microsoft.PowerShell--source winget如果想安装 MSI 版本winget install--id Microsoft.PowerShell--source winget--installer-typewix也可以从官方 GitHub Release 下载 MSI 安装包然后双击安装。个人比较推荐去官方网页直接下载MSI包https://learn.microsoft.com/zh-cn/powershell/scripting/install/install-powershell-on-windows?viewpowershell-7.6#msi安装时如果看到类似Add PowerShell to PATH的选项建议勾选。MSI 安装也支持ADD_PATH1参数用来把 PowerShell 加入 WindowsPATH环境变量。如果你使用自定义安装目录我的环境里 PowerShell 7 放在E:\PowerShell7\7\pwsh.exe这种情况下需要把目录加入系统PathE:\PowerShell7\7\注意加入的是文件夹不是pwsh.exe文件本身。操作路径系统属性 - 高级 - 环境变量 - 系统变量 - Path - 编辑 - 新建填入E:\PowerShell7\7\如下图所示一定要保证PowerShell7的路径在PowerShell5的上面这是codex优先调用PowerShell7的关键一步。因为默认情况下PowerShell7安装时间肯定更晚即使加入系统Path后位置也在PowerShell5的下面此时codex还是默认使用PowerShell5无法自动调用PowerShell7。重启 Codex 验证是否生效这一点很重要。修改系统Path后已经运行中的 Codex 进程通常不会自动拿到新的环境变量。需要完整退出并重新打开 Codex。重启前可能出现pwsh报错pwsh : The term pwsh is not recognized重启 Codex 后再验证。在 Codex 里执行Get-Process-Id$PID|Select-ObjectId,ProcessName,Path$PSVersionTable.PSVersion$PSHOME期望看到ProcessName: pwsh Path: E:\PowerShell7\7\pwsh.exe PSVersion: 7.x再执行Get-Commandpowershell,pwsh-ErrorAction SilentlyContinue|Select-ObjectName,CommandType,Source,Path|Format-List正常情况下会看到powershell.exe - C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe pwsh.exe - E:\PowerShell7\7\pwsh.exe这个结果说明pwsh已经可以调用 PowerShell 7powershell仍然是系统自带 PowerShell 5.1Codex 当前默认 shell 已经进入 PowerShell 7不建议依赖 shell_path 配置我也尝试过在 Codex 配置里写shell_path E:\\PowerShell7\\7\\pwsh.exe配置文件位置一般是C:\Users\用户名\.codex\config.toml但实测这个配置没有让当前本地命令通道稳定切到 PowerShell 7。OpenAI 官方 Codex 配置参考里也没有把shell_path列为公开配置项。所以更可靠的做法是安装 PowerShell 7把pwsh.exe所在目录加入系统Path且优先于Powershell 5重启 Codex用命令确认当前进程确实是pwsh常见问题1. 已经加了 Path为什么 Codex 还是找不到 pwsh通常是 Codex 还没有重启。可以先检查系统 Path[Environment]::GetEnvironmentVariable(Path,Machine)-split;|Where-Object{$_-matchPowerShell}也可以检查当前进程 Path$env:Path-split;|Where-Object{$_-matchPowerShell}如果系统 Path 有 PowerShell 7但当前进程 Path 没有重启 Codex 即可。2. PSModulePath 有用吗没有。启动pwsh.exe看的是Path不是PSModulePath。PSModulePath用来搜索 PowerShell 模块比如Import-Module。它不会决定启动 PowerShell 5 还是 PowerShell 7。3. 以后还会遇到中文或引号问题吗会明显减少但不能保证完全消失。但目前我自己使用下来没有再出现过codex主动说“又被Powershell咬了一口”的情况···实际查看运行日志基本全部调用pwsh命令且均执行成功。PowerShell 7 对 UTF-8、现代命令行行为、跨平台兼容性更好中文输出和脚本兼容性会稳定很多。但复杂命令仍可能经过多层解析Codex - PowerShell 7 - 外部程序 - 程序自己的参数解析下面这些场景仍要注意JSON 参数正则表达式中文路径多层嵌套引号node -epython -ccmd /cbash -lc遇到复杂命令时建议使用脚本文件、参数数组或 here-string减少一行命令里的引号嵌套。最终效果配置完成后在 Codex 中执行本地 shell 命令默认环境可以变成PowerShell 7.x E:\PowerShell7\7\pwsh.exe这能明显降低 Windows PowerShell 5.1 带来的编码、中文路径、引号解析和语法兼容问题。参考资料Microsoft PowerShell 7 Windows 安装文档https://learn.microsoft.com/en-us/powershell/scripting/install/install-powershell-on-windowsPowerShell 环境变量说明https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_environment_providerPSModulePath 说明https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_psmodulepathCodex 配置基础https://developers.openai.com/codex/config-basicCodex 配置参考https://developers.openai.com/codex/config-reference