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

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"
    }
  }
}
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值