You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell与VSCode UTF-8编码异常问题求助

解决VSCode中PowerShell调用应用的乱码问题

问题现象

  • 用PowerShell脚本调用第三方应用时,VSCode终端输出及脚本变量出现乱码(如路径中Automatic Upload显示为AutomΓǪ)
  • 该应用在VSCode外部生成的日志显示字符完全正常
  • 已将VSCode编码设置为UTF-8,但问题未解决

VSCode终端乱码输出示例

* My Pictures/Tony/Autom…22-07-13 16-43-54.heic: checking
 * My Pictures/Tony/Autom…22-07-14 11-03-00.heic: checking
 * My Pictures/Tony/Autom…22-07-14 11-03-01.heic: checking

正常应用日志示例

2022/07/22 12:39:38.790860 DEBUG : My Pictures/Tony/Automatic Upload/Tony’s iPhone/2021-12-09 19-36-19.heic: Excluded
2022/07/22 12:39:38.790860 DEBUG : My Pictures/Tony/Automatic Upload/Tony’s iPhone/2021-12-09 13-52-56.heic: Excluded
2022/07/22 12:39:38.790860 DEBUG : My Pictures/Tony/Automatic Upload/Tony’s iPhone/2021-12-24 23-41-10.jpeg: Excluded
2022/07/22 12:39:38.790860 DEBUG : My Pictures/Tony/Automatic Upload/Tony’s iPhone/2021-12-25 09-35-05.jpeg: Excluded
2022/07/22 12:39:38.790860 DEBUG : My Pictures/Tony/Automatic Upload/Tony’s iPhone/2021-12-25 09-25-11.jpeg: Excluded

VSCode版本信息

Version: 1.69.2 (user setup)
Commit: 3b889b090b5ad5793f524b5d1d39fda662b96a2a
Date: 2022-07-18T16:12:52.460Z
Electron: 18.3.5
Chromium: 100.0.4896.160
Node.js: 16.13.2
V8: 10.0.139.17-electron.0
OS: Windows_NT x64 10.0.19043

解决方法

1. 强制设置PowerShell输出编码为UTF-8

Windows下PowerShell默认输出编码为UTF-16LE,和VSCode终端期望的UTF-8不匹配,在脚本开头添加以下命令:

[Console]::OutputEncoding = [System.Text.UTF8Encoding]::UTF8
[System.Text.Encoding]::Default = [System.Text.UTF8Encoding]::UTF8

2. 同步VSCode终端相关编码配置

打开VSCode设置(快捷键Ctrl+,),确认以下配置:

  • terminal.integrated.defaultProfile.windows 选择PowerShell
  • terminal.integrated.encoding 设置为utf8
  • files.encoding 设置为utf8

3. 转换应用输出编码(若应用非UTF-8输出)

如果第三方应用本身输出GBK等其他编码,可在脚本中转换编码后输出:

$rawOutput = & .\your-app.exe | Out-String
$utf8Output = [System.Text.Encoding]::UTF8.GetString([System.Text.Encoding]::GetEncoding("GBK").GetBytes($rawOutput))
Write-Host $utf8Output

4. 升级VSCode到最新版本

你当前使用的1.69.2版本较旧,后续版本已修复部分终端编码相关问题,建议升级到最新稳定版后重试。


内容的提问来源于stack exchange,提问作者Tony

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 17:45:42