AI 改代码乱码
Cursor / Agent 改过的源码出现中文乱码,或终端里部分命令报错。常见原因:Windows 默认终端仍是 PowerShell 5.1,编码不是 UTF-8,且不支持 && 等较新语法。
处理顺序:安装 PowerShell 7 → 写入 UTF-8 Profile → Cursor 默认终端改成 pwsh → 彻底重启 Cursor。
现象
- AI 写入或改过的文件,中文变成问号、方块或错位字符
- 终端输出的中文路径、git 信息乱码
- Agent 跑的命令在 5.1 下语法失败(例如
&&)
原因
| 项 | PowerShell 5.1 | PowerShell 7 |
|---|---|---|
| 可执行文件 | powershell.exe(系统自带) | pwsh.exe(需安装) |
| 默认编码 | 偏系统代码页(中文 Windows 常为 GBK) | UTF-8 |
| 与 5.1 关系 | — | 并存,不会覆盖 5.1 |
源码一般按 UTF-8 读写;5.1 默认按 GBK / UTF-16 写文件时就会乱码。
1. 确认当前版本
$PSVersionTable.PSVersion
Get-Command powershell, pwsh -ErrorAction SilentlyContinue | Select-Object Name, SourceMajor 为 5 即仍在用 5.1。已装 7 时,pwsh 应能解析到真实 exe。
Agent 侧查找 pwsh 时,Microsoft Store / WindowsApps 别名经常解析失败,会回退到 5.1。安装后执行:
where.exe pwsh应指向 C:\Program Files\PowerShell\7\pwsh.exe(或同类 Program Files 路径),而不是 %LOCALAPPDATA%\Microsoft\WindowsApps\pwsh.exe。
2. 安装 PowerShell 7
推荐 winget(装的是 MSI,路径一般在 Program Files):
winget search Microsoft.PowerShell
winget install --id Microsoft.PowerShell --source winget若 where.exe pwsh 仍落在 WindowsApps,卸载后改用 GitHub 的 .msi:https://github.com/PowerShell/PowerShell/releases(选 PowerShell-*-win-x64.msi)。
启动并确认版本:
pwsh
$PSVersionTable.PSVersion日常升级:
winget upgrade --id Microsoft.PowerShell --silent3. 写入 UTF-8 Profile
即使已是 7,Out-File / Set-Content 仍可能用错编码。在 pwsh 里执行下面脚本,写入当前会话的 $PROFILE(5.1 与 7 路径不同,要在对应版本里各跑一次)。
先切代码页,再装配置:
chcp 65001 | Out-Null
$utf8Config = @'
chcp 65001 | Out-Null
[Console]::InputEncoding = [System.Text.UTF8Encoding]::new()
[Console]::OutputEncoding = [System.Text.UTF8Encoding]::new()
$OutputEncoding = [System.Text.UTF8Encoding]::new()
$PSDefaultParameterValues['Out-File:Encoding'] = 'utf8'
$PSDefaultParameterValues['Set-Content:Encoding'] = 'utf8'
$PSDefaultParameterValues['Add-Content:Encoding'] = 'utf8'
'@
$profileDir = Split-Path -Parent $PROFILE
if (-not (Test-Path $profileDir)) {
New-Item -ItemType Directory -Path $profileDir -Force | Out-Null
}
$existing = if (Test-Path $PROFILE) { Get-Content $PROFILE -Raw } else { '' }
$hasConfig = $existing -match 'Console::OutputEncoding' -and
$existing -match '\$OutputEncoding' -and
$existing -match 'PSDefaultParameterValues.*Out-File:Encoding'
if (-not $hasConfig) {
$newContent = if ($existing) { "$existing`r`n$utf8Config" } else { $utf8Config }
$utf8NoBom = New-Object System.Text.UTF8Encoding($false)
[System.IO.File]::WriteAllText($PROFILE, $newContent, $utf8NoBom)
Write-Host "UTF-8 已写入 Profile: $PROFILE"
} else {
Write-Host "UTF-8 已配置,跳过。"
}当前窗口立刻生效:
. $PROFILE若提示禁止运行脚本:
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned -Force| 版本 | 典型 $PROFILE 路径 |
|---|---|
| 5.1 | Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 |
| 7 | Documents\PowerShell\Microsoft.PowerShell_profile.ps1 |
4. Cursor 默认终端改成 PowerShell 7
用户设置 JSON(Ctrl+Shift+P → Preferences: Open User Settings (JSON))里合并下列键(不要整文件覆盖;路径以本机 where.exe pwsh 为准):
"terminal.integrated.defaultProfile.windows": "PowerShell",
"terminal.integrated.profiles.windows": {
"PowerShell": {
"source": "PowerShell",
"path": "C:\\Program Files\\PowerShell\\7\\pwsh.exe"
}
},
"terminal.integrated.automationProfile.windows": {
"path": "C:\\Program Files\\PowerShell\\7\\pwsh.exe"
}然后 关掉全部终端,并完全退出再打开 Cursor(不要只关窗口;Agent 用的 Shell 会缓存)。
集成终端正常、Agent 仍是 5.1 时:确认 pwsh 不在 WindowsApps;仍不行可在 Cursor 设置里打开 Legacy Terminal Tool(Agents / Inline Editing & Terminal)后再重启一次。
5. 验证
新开终端(以及让 Agent 执行同一条)应看到 7.x、UTF-8、代码页 65001:
$PSVersionTable.PSVersion
[Console]::OutputEncoding
$OutputEncoding
chcp再用 Agent 改一个带中文注释的文件,确认不再乱码、命令不再因 && 失败。
可选:打开 5.1 时自动进入 7
只在 5.1 的 Profile 里加(在 5.1 会话中编辑 $PROFILE):
if ($PSVersionTable.PSVersion.Major -lt 7) {
pwsh
exit
}这只影响你手动开的 5.1 窗口;Agent 仍取决于第 4 步能否解析到 pwsh。