CodeX的乱码问题

Windows 下从 PowerShell 5.1 切换到 PowerShell 7(pwsh),彻底解决 UTF-8 中文乱码问题.

最近再使用codex的时候发现了乱码问题老师解决不了,交互的时候有很大问题,一,好像乱码会影响codex的思考,二乱码,我看不懂分析过程了。

所以痛下决心,一定要改好,下面是教程。

一、问题背景

在 Windows 下使用 PowerShell + Node / Python / 前端开发 时,经常会遇到下面这些问题:

Get-Content 读取 JS / Python 文件时中文注释乱码

明明设置了 UTF-8,输出却变成 纭繚、鏂囦欢

PowerShell 配置了一堆编码参数,效果仍然不稳定

一开始以为是自己配置有问题,后来才发现:

根本原因是 Windows 自带的 PowerShell 版本太老(5.1)

二、问题根因:Windows 自带的是 PowerShell 5.1

先看一下当前 PowerShell 版本:

$PSVersionTable.PSVersion

如果输出类似:

Major Minor

5 1

说明你用的是 Windows PowerShell 5.1(系统自带)。

⚠️ 重点

PowerShell 5.1 无法升级到 7

PowerShell 7(pwsh)只能 单独安装,与 5.1 并存

三、安装 PowerShell 7(pwsh)

方法一:使用 winget(推荐)

在 PowerShell 5.1 中执行:

复制代码
winget install --id Microsoft.PowerShell -e

安装完成后,关闭所有终端窗口并重新打开。

验证是否安装成功

复制代码
pwsh -v

如果输出类似:

复制代码
PowerShell 7.x.x

说明 PowerShell 7 已安装成功。

四、常见坑:pwsh 能运行,但系统找不到

有时会出现这种情况:

复制代码
where pwsh

没有任何输出

复制代码
pwsh

却能正常进入 PowerShell 7

说明:

pwsh 已安装

但 没有被 Windows Terminal / VS Code 自动识别

解决方法是:手动指定 pwsh.exe 的路径。

五、Windows Terminal 设置默认使用 PowerShell 7

1️⃣ 获取 pwsh.exe 的真实路径

复制代码
(Get-Command pwsh).Source

一般结果是:

复制代码
C:\Program Files\PowerShell\7\pwsh.exe

2️⃣ 在 Windows Terminal 中手动添加 Profile

打开 Windows Terminal

按 Ctrl + , 打开设置

左侧选择 Profiles → Add a new profile → Empty profile

填写以下内容:

Name:PowerShell 7

Command line:

复制代码
C:\Program Files\PowerShell\7\pwsh.exe

Starting directory(可选):

%USERPROFILE%

点击 Save

3️⃣ 设置为默认 Profile

回到设置首页

Default profile 选择 PowerShell 7

保存并重启 Windows Terminal

验证

复制代码
$PSVersionTable.PSVersion

如果看到 7.x,说明切换成功。

六、VS Code 中设置默认终端为 PowerShell 7

方法一:图形界面

打开 VS Code

复制代码
Ctrl + ,

搜索 terminal default profile

在 Windows 下选择 PowerShell / PowerShell 7

方法二:手动编辑 settings.json(最稳定)

复制代码
"terminal.integrated.profiles.windows": {
  "PowerShell 7": {
    "path": "C:\\Program Files\\PowerShell\\7\\pwsh.exe"
  }
},
"terminal.integrated.defaultProfile.windows": "PowerShell 7"

保存后重启 VS Code。

七、为什么 PowerShell 7 能彻底解决 UTF-8 乱码

对比总结:

项目 PowerShell 5.1 PowerShell 7

默认编码 ANSI / GBK UTF-8

Get-Content 容易乱码 正常

Node / Python 行为不一致 一致

开发体验 很差 很好

PowerShell 7 基于 .NET Core,对 UTF-8 的支持是现代化的,

大多数情况下 无需再手动设置编码参数。

八、总结

Windows 下做开发,强烈建议直接使用 PowerShell 7(pwsh),

不要再和 PowerShell 5.1 的编码问题硬刚。

切换完成后:

中文注释不再乱码

终端行为和 Linux / macOS 更一致

Node / Python / Git 使用体验明显提升

早点换 pwsh,早点解脱。

相关推荐
?re?ta?rd?ed?12 小时前
C++11新特性,{}与左值
c++·windows
必须得开心呀15 小时前
windows10系统任务栏图标消失修复
windows
zl_dfq16 小时前
Windows 下 UDP 套接字编程指南
网络·windows·udp
学习论之费曼学习法16 小时前
无人值守AI内容矩阵实战(10):Windows 上让 Python 进程 7×24 活着
人工智能·windows·python·矩阵·powershell·进程守护
少年的范儿17 小时前
Windows 安装 MySQL 8.4 教程(ZIP 解压版):配置 my.ini、初始化数据库与启动服务
数据库·windows·mysql
胖纸不争17 小时前
EnergyStarZ:让 Windows 后台进程给前台让路的能效管理器(v1.2 发布)
windows·性能优化·c#·win32·ef core
2501_9159090619 小时前
Windows 上用 VS Code 调试 iPhone 真机 App,KXBuild 断点、崩溃定位实操
ide·windows·vscode·ios·iphone·个人开发·敏捷流程
情 九19 小时前
MCP 开发环境搭建:Mac 与 Windows 跑通 Python 服务和 stdio 客户端
windows·python·macos·mcp
懂点Tech1 天前
ctfshow misc28:GIF 高度修改解题与脚本实现
开发语言·windows·python
云梦谭1 天前
让 Linux 主动连接 Windows:配置 Windows 11 SSH 服务端
linux·windows·ssh