UTF-8 output from PowerShell UTF-8 output from PowerShell powershell powershell

UTF-8 output from PowerShell


Not an expert on encoding, but after reading these...

... it seems fairly clear that the $OutputEncoding variable only affects data piped to native applications.

If sending to a file from withing PowerShell, the encoding can be controlled by the -encoding parameter on the out-file cmdlet e.g.

write-output "hello" | out-file "enctest.txt" -encoding utf8

Nothing else you can do on the PowerShell front then, but the following post may well help you:.


This is a bug in .NET. When PowerShell launches, it caches the output handle (Console.Out). The Encoding property of that text writer does not pick up the value StandardOutputEncoding property.

When you change it from within PowerShell, the Encoding property of the cached output writer returns the cached value, so the output is still encoded with the default encoding.

As a workaround, I would suggest not changing the encoding. It will be returned to you as a Unicode string, at which point you can manage the encoding yourself.

Caching example:

102 [C:\Users\leeholm]>> $r1 = [Console]::Out103 [C:\Users\leeholm]>> $r1Encoding                                          FormatProvider--------                                          --------------System.Text.SBCSCodePageEncoding                  en-US104 [C:\Users\leeholm]>> [Console]::OutputEncoding = [System.Text.Encoding]::UTF8105 [C:\Users\leeholm]>> $r1Encoding                                          FormatProvider--------                                          --------------System.Text.SBCSCodePageEncoding                  en-US


Set the [Console]::OuputEncoding as encoding whatever you want, and print out with [Console]::WriteLine.

If powershell ouput method has a problem, then don't use it. It feels bit bad, but works like a charm :)