#一、 配置 PowerShell 启动脚本自动切换 Code Page
在 PowerShell 配置文件中添加 chcp 65001
命令,让它每次启动时都切换编码。
1. 打开或创建配置文件:
notepad $PROFILE
如果提示文件不存在,先运行:
New-Item -ItemType File -Path $PROFILE -Force
2. 添加如下内容:
chcp 65001 > $null
下次打开 PowerShell 就会自动应用 UTF-8 编码。
一键运行脚本
@echo off
:: 设置 UTF-8 编码的 PowerShell 启动脚本
echo 正在设置 PowerShell 默认编码为 UTF-8...
:: 获取 PowerShell 配置文件路径
PowerShell.exe -Command "if (!(Test-Path -Path $PROFILE)) { New-Item -ItemType File -Path $PROFILE -Force }" >nul 2>&1
:: 添加 chcp 65001 到配置文件
PowerShell.exe -Command "Add-Content -Path $PROFILE -Value 'chcp 65001 > $null'" >nul 2>&1
echo.
:: 验证 PowerShell 编码是否为 UTF-8
PowerShell.exe -Command "$encoding = [Console]::OutputEncoding.CodePage; if ($encoding -eq 65001) { Write-Host '验证成功:PowerShell 默认编码已设置为 UTF-8 (65001)。' } else { Write-Host '验证失败:PowerShell 默认编码为 ' + $encoding + ',不是 UTF-8 (65001)。' }" >nul 2>&1
echo.
echo 成功设置!现在每次打开 PowerShell 都会默认使用 UTF-8 编码。
echo 请尝试重新打开一个新的 PowerShell 窗口以查看效果。
echo.
pause
OK啦。。。
资源已上传,自取