What does the redirection operator > in PowerShell do to text: leave it as Unicode or convert it to ASCII? The answer depends on whether the thing to the right of the
> operator is a file or a program.
Strings inside PowerShell are 16-bit Unicode, instances of .NET’s
System.String class. When you redirect the output to a file, the file receives Unicode text. As Bruce Payette says in his book Windows PowerShell in Action,
myScript > file.txtis just syntactic sugar for
myScript | out-file -path file.txt
out-file defaults to Unicode. The advantage of explicitly using
out-file is that you can then specify the output format using the
-encoding parameter. Possible encoding values include Unicode, UTF8, ASCII, and others.
If the thing on the right side of the redirection operator is a program rather than a file, the encoding is determined by the variable
$OutputEncoding. This variable defaults to ASCII encoding because most existing applications do not handle Unicode correctly. However, you can set this variable so PowerShell sends applications Unicode. See Jeffrey Snover’s blog post OuputEncoding to the rescue for details.
Of course if you’re passing strings between pieces of PowerShell code, everything says in Unicode.
Thanks to J_Tom_Moon_79 for suggesting a blog post on this topic.