219 字
1 分钟
PowerShell 中文乱码修复方案
PowerShell 中文乱码是 Windows 老问题,AI 工具(OpenCode、Codex CLI 等)中尤其严重——常规的 $PROFILE 修复方案对它们无效,因为这些工具启动 PowerShell 时带 -NoProfile 参数。
问题根源是两个编码变量:$OutputEncoding 默认 US-ASCII(管道中文变问号),[Console]::OutputEncoding 默认 GB2312。AI 工具按 UTF-8 解码 stdout,PowerShell 却按 GB2312 编码输出,必乱。
方案一:自动
发给 agent 执行即可:
# 注册表:绕过 -NoProfile,让 PowerShell 默认输出 UTF-8$key = "HKCU:\Console\%SystemRoot%_System32_WindowsPowerShell_v1.0_powershell.exe"New-ItemProperty -Path $key -Name "CodePage" -Value 65001 -PropertyType DWord -Force | Out-Null
# Profile:交互式终端的双重保障$profileDir = Split-Path $PROFILE -Parentif (-not (Test-Path $profileDir)) { New-Item -Path $profileDir -ItemType Directory -Force | Out-Null }@"`$OutputEncoding = [System.Text.UTF8Encoding]::new(`$false)[System.Console]::OutputEncoding = [System.Text.UTF8Encoding]::new(`$false)[System.Console]::InputEncoding = [System.Text.UTF8Encoding]::new(`$false)"@ | Out-File -FilePath $PROFILE -Encoding utf8 -Append
# Python 环境变量[Environment]::SetEnvironmentVariable("PYTHONIOENCODING", "utf-8", "User")
Write-Host "配置完成,重启终端生效" -ForegroundColor Green方案二:手动
# 一行搞定注册表New-ItemProperty -Path "HKCU:\Console\%SystemRoot%_System32_WindowsPowerShell_v1.0_powershell.exe" -Name "CodePage" -Value 65001 -PropertyType DWord -Force只影响当前用户的 PowerShell,不碰系统全局代码页。注册表是进程级默认值,-NoProfile 下也生效。
可选步骤——Profile 添加:
$OutputEncoding = [System.Text.UTF8Encoding]::new($false)[System.Console]::OutputEncoding = [System.Text.UTF8Encoding]::new($false)[System.Console]::InputEncoding = [System.Text.UTF8Encoding]::new($false)Python 环境变量:设置 → 系统 → 高级系统设置 → 环境变量 → 新建,变量名 PYTHONIOENCODING,值 utf-8。
验证
echo "你好世界"影响
findstr 管道匹配中文会失效,改用 Select-String。其余场景全部正常。回滚只需删除注册表中 CodePage 值。
分享
如果这篇文章对你有帮助,欢迎分享给更多人!
PowerShell 中文乱码修复方案
https://blog.nth2miss.cn/posts/powershell-utf8-encoding-fix/ 部分信息可能已经过时
相关文章 智能推荐
1
浏览器卡屏/切换页面卡住问题修复
疑难杂解 浏览器切换页面上半部分卡死,只有下半部分正常显示
2
jsDelivr 使用指南
实用技巧 jsDelivr 是一个非常强大且快速的 免费开源 CDN。它不仅可以加速常见的 npm 包,还能直接作为 GitHub 仓库的静态资源代理。
3
用户注册登录系统
技术 使用 Flask 作为后端和 Vue 作为前端的用户认证系统,支持邮箱注册和登录功能,包含验证码验证
4
Github Proxy Download 浏览器插件
使用指南 github release、archive以及项目文件的加速项目的浏览器插件
5
R2/S3 管理面板
使用指南 一个基于 Cloudflare Pages + Hono 构建的轻量级 R2/S3 兼容对象存储管理面板