VS Code Code Runner运行PowerShell输出乱码含不可读字符求助
PowerShell脚本在VS Code Code Runner中输出乱码问题解决
问题现象
- 使用VS Code搭配Code Runner扩展运行PowerShell脚本时,脚本可正常执行,但输出存在乱码:德语变音字符显示异常,还出现奇怪的不可读字符。
- 已尝试在settings.json中配置Code Runner使用代码页1252:
但问题未解决。"code-runner.executorMap": { "powershell": "chcp 1252 & pwsh -ExecutionPolicy ByPass -File"} - 脚本文件均为UTF-8编码,直接在终端运行脚本时,输出显示正常。
解决方案
1. 强制PowerShell输出UTF-8编码
修改Code Runner的执行配置,让PowerShell以UTF-8编码输出内容:
"code-runner.executorMap": { "powershell": "pwsh -ExecutionPolicy Bypass -File $fullFileName -OutputEncoding utf8" }
同时确保Code Runner的编码设置为UTF-8:
"code-runner.encoding": "utf8"
2. 让Code Runner在终端中运行
设置Code Runner直接在VS Code集成终端中执行脚本,继承终端的编码环境:
"code-runner.runInTerminal": true
这种方式和手动在终端运行脚本的环境一致,能避免编码不匹配的问题。
3. 配置PowerShell默认输出编码
在PowerShell中设置默认输出编码为UTF-8,执行以下命令可临时生效:
$OutputEncoding = [Console]::OutputEncoding = [System.Text.UTF8Encoding]::new()
若要永久生效,将上述命令添加到PowerShell配置文件(执行$PROFILE查看文件路径)中即可。
内容的提问来源于stack exchange,提问作者Claus
相关产品推荐
相关产品推荐

