While working with PowerShell, I found that echoing Japanese directly in the console doesnโt cause garbled text, but executing a script that contains Japanese results in garbled output. This article introduces how to deal with character encoding issues when running PowerShell scripts.
# test.ps1
echo ใ
PS C:\pg\selenium> echo ใ
ใ
PS C:\pg\selenium> . test.ps1
็ธบใป
PS C:\pg\selenium> . test.ps1
ใ
Solution: Add BOM
nkf --overwrite --oc=UTF-8-BOM test.ps1
utf 8 - UTF8 Script in PowerShell outputs incorrect characters - Stack Overflow
When garbled text occurs as above, adding BOM using nkf will solve it.
nkf is a command that converts character encoding.
When using nkf, you can add BOM by adding the --oc=UTF-8-BOM option.
Note that the character encoding is different between utf-8 and utf-8-bom. utf-8-bom is utf-8 with BOM added. BOM is used to identify the character encoding. By adding BOM, you can identify the character encoding, which can prevent garbled text.