Powershell 7.x中UTF-8环境中文乱码解决办法

本文详细介绍了如何在PowerShell7中临时和永久解决中文显示问题,包括设置输出编码、修改profile文件、执行权限提升及检查字符集,同时推荐了支持中文的FiraCode Nerd Font Mono Retina字体和VSCode配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.临时解决办法(重启pw失效,以下Powershell7 简称pw):

pw终端中输入以下:

 $OutputEncoding = [console]::InputEncoding = [console]::OutputEncoding = [System.Text.Encoding]::GetEncoding(936);

 2.永久解决办法:

修改方法:

1、以管理员身份打开powershell,运行下面代码

New-Item $PROFILE  -ItemType File -Force

2、 打开C盘,找到我的文档中的WindowsPowerShell文件夹

3、编辑这个ps1文件(默认是空的),加上以下代码

英文使用utf-8:

$OutputEncoding = [console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding

中文使用:

 $OutputEncoding = [console]::InputEncoding = [console]::OutputEncoding = [System.Text.Encoding]::GetEncoding(936);


4、以管理员身份打开powershell,运行下面代码

Set-ExecutionPolicy Unrestricted

5、打开powershell,输入chcp,查看代码活动页是否是 936(GBK) 和 65001 (UTF-8)
 

如果上述操作后仍然无法正常显示中文,应该是没有正确配置中文字体。推荐安装windows terminal,设置等宽且支持中文的字体。推荐支持Retina的等宽字体FiraCode Nerd Font Mono Retina

 如果在VSCode中无法正常显示,参考以下配置:

{
  "terminal.external.windowsExec": "C:\\Program Files\\PowerShell\\7\\pwsh.exe --nologo",
  "editor.fontFamily": "'FiraCode Nerd Font Mono Retina', 'Microsoft YaHei Mono', Consolas, 'Courier New', monospace",
  "terminal.integrated.minimumContrastRatio": 1,
  "terminal.integrated.defaultProfile.windows": "PowerShell",
  "terminal.integrated.profiles.windows": {
    "PowerShell": {
      "path": "C:\\Program Files\\PowerShell\\7\\pwsh.exe",
      "args": [
        "--nologo"
      ],
      "source": "PowerShell",
      "icon": "terminal-powershell"
    }
  }
}
<project> <description>test Upgrade模板</description> <keepDependencies>false</keepDependencies> <properties/> <scm class="hudson.scm.NullSCM"/> <canRoam>true</canRoam> <disabled>false</disabled> <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding> <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding> <!-- 已移除定时触发器 --> <!-- 原始触发器配置已删除 --> <concurrentBuild>false</concurrentBuild> <customWorkspace>D:/Jenkins_new</customWorkspace> <builders> <!-- 阶段交换:原第二阶段提到前面 --> <hudson.tasks.BatchFile> <command><![CDATA[ curl -s http://${agentHost}:${agentPort}/beforeUpgrade?jobName=%JOB_BASE_NAME% chcp 65001 @echo on cd %WORKSPACE%/%JOB_BASE_NAME%/ curl -s -X POST -H "Content-Type: application/json" -d "${paramsStr}" http://${agentHost}:${agentPort}/afterUpgrade ]]></command> </hudson.tasks.BatchFile> <!-- 原第一阶段变为第二阶段 --> <hudson.tasks.BatchFile> <command><![CDATA[ @echo off chcp 65001 > nul :LOOP REM 获取并过滤日志 curl -s http://${agentHost}:${agentPort}/logs?serialNumber=${serialNumber} | findstr ${serialNumber} > filtered_log.txt if %errorlevel% equ 0 ( type filtered_log.txt ) else ( echo No matching logs found ) REM 检查服务状态 curl -o status.txt -s -w "%%{http_code}" http://${agentHost}:${agentPort}/status?serialNumber=${serialNumber} set /p status=<status.txt if "%status%" == "200" ( echo Service returned success status. Stopping... exit 0 ) else ( echo Service not ready. Retrying in 10 seconds... ping 127.0.0.1 -n 11 > nul goto LOOP ) ]]></command> </hudson.tasks.BatchFile> </builders> </project> 帮我解决第二阶段jenkins控制台输出中文乱码问题,log接口返回的是"UTF-8"格式
03-13
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值