PowerShell on Legacy Chinese Windows: UTF-8 with BOM or Bust
Windows PowerShell 5.1 does not guess encodings. When it loads a .ps1 file, it looks for a byte-order mark. If it finds one, it decodes accordingly. If it does not, it falls back to the system's ANSI code page — and on a Chinese Windows install, that code page is GBK (CP936). Every non-ASCII character in your script is then decoded through the wrong table, and you get mojibake that does not throw…
Windows PowerShell 5.1 does not automatically detect file encodings. When loading a .ps1 file, it looks for a byte-order mark. If present, PowerShell decodes the file accordingly. Without a BOM, it defaults to the system's ANSI code page. On Chinese Windows installations, this code page is GBK (CP936). As a result, non-ASCII characters in the script are decoded through the wrong table, causing mojibake that does not generate an error.
The issue led to a week of debugging after a deployment script created directories with incorrect names due to the misinterpretation of characters. The key to resolving the problem is ensuring all .ps1 files are saved as UTF-8 with a BOM and CRLF endings. Identifying the issue involves checking the first bytes of the file; a BOM like EF BB BF indicates UTF-8 with a BOM, while PowerShell 5.1 falls back to the ANSI code page without one.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.