ArticlesPending human review
đ€ The PowerShell Chinese Encoding Trap â Why Your Files Come Out Garbled
The Symptom You write a PowerShell script that outputs Chinese text to a file. You open the file. Garbage. Not corrupted â wrong encoding. Layer 1: PowerShelâŠ

The Symptom
You write a PowerShell script that outputs Chinese text to a file. You open the file. Garbage. Not corrupted â wrong encoding.
Layer 1: PowerShell's Default Output Encoding
PowerShell defaults to the system's active code page â often GBK on Chinese Windows, or UTF-16 LE with BOM. Neither is what you want if other tools expect UTF-8.
Fix at the top of your script:
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$OutputEncoding = [System.Text.Encoding]::UTF8
Layer 2: Out-File vs Set-Content
Out-File in older PowerShell uses UTF-16 LE with BOM by default. Use Set-Content with explicit encoding instead:
Set-Content -Path "output.txt" -Value $content -Encoding UTF8
Layer 3: BOM
UTF-8 with BOM adds three invisible bytes. Most Linux tools and APIs don't handle it. For cross-platform files, use UTF-8 without BOM:
[System.IO.File]::WriteAllText(
(Resolve-Path "output.txt"),
$content,
[System.Text.UTF8Encoding]::new($false) # false = no BOM
)