A `.ps1` file that contained non-ASCII text — Chinese comments and Chinese `Write-Host`
A .ps1 file that contained non-ASCII text — Chinese comments and Chinese Write-Host messages — failed on Windows with a cascade of parse errors that pointed at innocent lines:
Unexpected token ')' in expression or statement # on a function/section call
Unexpected token # on a Write-Host line
Missing closing '}' in statement block
Try statement is missing its Catch or Finally blockThe try/catch pair was complete and balanced. The only real clue was inside the error text itself: the quoted source lines contained characters that were never in the file (mojibake). Because the errors were reported at line numbers that did not correspond to the offending bytes, the failure read like broken script logic, and each round of "fix the line the error names" changed nothing.
Windows PowerShell 5.1 (powershell.exe) does not sniff for UTF-8 without a BOM. Unless a .ps1 starts with a UTF-8 BOM, or is UTF-16LE, the engine decodes it with the system ANSI code page — GBK/CP936 on a Chinese Windows. Most modern editors and agent file-writing tools write UTF-8 without a BOM by default, which is exactly the shape that gets mis-decoded.
Two consequences follow, and both matter:
So the parser messages are a symptom of the decode, not a verdict about PowerShell syntax. The same file can also "work on one machine and not another": PowerShell 7 (pwsh) reads UTF-8 by default and never shows this failure, while a host that only has powershell.exe always does.
Either fix is sufficient.
Write comments and console output in English and avoid non-ASCII characters completely. No BOM is needed, and the file is portable across every PowerShell version and code page. In the reported case, rewriting the same repair script in pure ASCII ran to completion on the first try, with no other change.
Save the file as UTF-8 with BOM (or UTF-16LE), or run it under PowerShell 7:
# PowerShell 5.1: write the file through .NET so a BOM is actually emitted
$text = Get-Content -Raw -LiteralPath <path>
[System.IO.File]::WriteAllText(<path>, $text, (New-Object System.Text.UTF8Encoding($true)))
# or just use the engine that reads UTF-8 by default
pwsh -File <path>A tool description that says pwsh -Command does not guarantee PowerShell 7 is what ran:
$PSVersionTable.PSVersion # 5.1.x => Windows PowerShell
$PSVersionTable.PSEdition # Desktop => not Core
Get-Command pwsh -ErrorAction SilentlyContinue # absent => no PowerShell 7 on this hostReproduced on Windows 11 with Windows PowerShell 5.1.26100.9168 and a Chinese (GBK) system code page: the bilingual version aborted with the parser cascade above; the ASCII-only version of the same script — same logic, same commands — ran end to end and printed its final DONE line. The failures were also confirmed to be reported at the wrong line numbers, so the error text itself is not a reliable pointer to the defect.
Do not verify by watching whether "the error moved". Verify by the script's own success output: run the repaired file and check that it reaches its last statement.
.ps1 file input. An inline -Command string is decoded through the console code page instead, which is a different trap with different symptoms.lessons/contrib/tts-chinese-encoding-powershell.md and lessons/contrib/windows-acl-write-sandbox-grantwrite-win32-5.md, which both recommend keeping generated scripts strictly ASCII for this reason.powershell.exe on current Windows builds, so "the file is valid UTF-8" is not enough — the encoding declaration is part of the program.