Change OutputEncoding and code page
I was having some trouble getting Unicode output with PowerShell, which I partially solved using this:
[Console]::OutputEncoding = [Text.Encoding]::UTF8However I noticed that this setting has an issue. If I run a command like this:
Get-Content a.txtIt does not output in UTF8. It seems this is the reason:
Default Uses the encoding that corresponds to the system's active code page (usually ANSI).
So if [Console]::OutputEncoding doesnt change the Code Page, then what does?
2 Answers
If [Console]::OutputEncoding doesn't change the Code Page, then what does?
According to Set the default encoding to UTF-8 in powershell you should be using:
[Console]::OutputEncoding = [Text.UTF8Encoding]::UTF8
Which is slightly different from the command you are using.
Another way is to use chcp.com:
& "$env:windir\system32\chcp.com" 65001
After testing it seems like powershell likes to work with BOM encoded UTF8 files.
If your files are not BOM encoded you can use -encoding utf8.
Example with a non BOM encoded UTF8 file:
> get-content test.txt
Greek (in Polytonic): The Greek anthem: Σὲ γνωÏίζω ἀπὸ τὴν κόψη τοῦ σπαθιοῦ τὴν Ï„ÏομεÏá½µ, σὲ γνωÏίζω ἀπὸ τὴν ὄψη ποὺ μὲ βία μετÏάει τὴ γῆ. ᾿Απ᾿ τὰ κόκκαλα βγαλμένη τῶν ῾Ελλήνων τὰ ἱεÏá½± καὶ σὰν Ï€Ïῶτα ἀνδÏειωμένη χαῖÏε, ὦ χαῖÏε, ᾿ΕλευθεÏιά!
> get-content test.txt -encoding utf8
Greek (in Polytonic): The Greek anthem: Σὲ γνωρίζω ἀπὸ τὴν κόψη τοῦ σπαθιοῦ τὴν τρομερή, σὲ γνωρίζω ἀπὸ τὴν ὄψη ποὺ μὲ βία μετράει τὴ γῆ. ᾿Απ᾿ τὰ κόκκαλα βγαλμένη τῶν ῾Ελλήνων τὰ ἱερά καὶ σὰν πρῶτα ἀνδρειωμένη χαῖρε, ὦ χαῖρε, ᾿Ελευθεριά!
> 2 Browse to this key:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Nls\CodePageand change OEMCP to 65001. Then restart. With this fix, if you are using
Consolas font, it seems to lock PowerShell into a small font size. cmd.exestill works fine. As a workaround, you can use Lucida Console, or I switched
to Cascadia Mono: