中文编码处理工具包,解决 PowerShell 环境下中文文件读写乱码问题。提供自动编码检测、安全文件读写和终端显示修复功能。
powershell
.\scripts\encoding-detector.ps1 -Path "C:\path\to\file.txt"
输出示例:
text
文件路径:C:\path\to\file.txt
检测编码:UTF-8-BOM
置信度:100%
powershell
.\scripts\safe-read.ps1 -Path "C:\path\to\file.txt"
带编码参数读取:
powershell
.\scripts\safe-read.ps1 -Path "C:\path\to\file.txt" -Encoding "UTF-8"
powershell
.\scripts\safe-write.ps1 -Path "C:\path\to\file.txt" -Content "中文内容"
指定编码写入:
powershell
.\scripts\safe-write.ps1 -Path "C:\path\to\file.txt" -Content "中文内容" -Encoding "UTF-8-BOM"
powershell
.\scripts\terminal-fix.ps1
永久修复(需要管理员权限):
powershell
.\scripts\terminal-fix.ps1 -Permanent
EF BB BFFF FEFE FF系统支持程度备注Windows PowerShell 5.1✅ 完全支持推荐 UTF-8-BOMWindows PowerShell 7+✅ 完全支持默认 UTF-8Linux/macOS✅ 支持建议无 BOM UTF-8CI/CD 环境✅ 支持需确保终端 UTF-8
Q1: 为什么读取文件还是乱码?
-Encoding 参数encoding-detector.ps1 重新检测Q2: 写入的文件在记事本打开乱码?
-Encoding "UTF-8-BOM" 参数Q3: PowerShell 终端显示中文为方框?
terminal-fix.ps1-Permanent 参数永久修复Q4: 批量处理文件时部分失败?
错误码含义解决方案ERR-001文件不存在检查路径是否正确ERR-002权限不足以管理员身份运行ERR-003编码检测失败手动指定编码参数ERR-004文件被占用关闭占用程序后重试ERR-005终端设置失败检查注册表权限