• 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中无法正常显示,参考以下配置:

    1. {
    2. "terminal.external.windowsExec": "C:\\Program Files\\PowerShell\\7\\pwsh.exe --nologo",
    3. "editor.fontFamily": "'FiraCode Nerd Font Mono Retina', 'Microsoft YaHei Mono', Consolas, 'Courier New', monospace",
    4. "terminal.integrated.minimumContrastRatio": 1,
    5. "terminal.integrated.defaultProfile.windows": "PowerShell",
    6. "terminal.integrated.profiles.windows": {
    7. "PowerShell": {
    8. "path": "C:\\Program Files\\PowerShell\\7\\pwsh.exe",
    9. "args": [
    10. "--nologo"
    11. ],
    12. "source": "PowerShell",
    13. "icon": "terminal-powershell"
    14. }
    15. }
    16. }
  • 相关阅读:
    Linux安装MySQL
    9. xaml ComboBox控件
    基于STM32的智能小车方案设计
    Flink Operator 使用指南 之 Flink Operator安装
    排序:快速排序算法分析
    同花顺_代码解析_技术指标_L
    Windows下通过Ollama部署使用本地模型
    14个SpringBoot优化小妙招
    vue3+elementPlus:el-tree复制粘贴数据功能,并且有弹窗组件
    SpringAOP执行流程——从源码画流程图
  • 原文地址:https://blog.csdn.net/shadow_zed/article/details/126396983