且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PowerShell的UTF-8输出

更新时间:2022-11-07 12:15:52

净。当PowerShell启动时,它缓存输出句柄(Console.Out)。

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.

当从PowerShell中更改它时,缓存输出作者的Encoding属性返回缓存的值,因此输出仍使用默认编码进行编码。

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.

缓存示例:

102 [C:\Users\leeholm]
>> $r1 = [Console]::Out

103 [C:\Users\leeholm]
>> $r1

Encoding                                          FormatProvider
--------                                          --------------
System.Text.SBCSCodePageEncoding                  en-US



104 [C:\Users\leeholm]
>> [Console]::OutputEncoding = [System.Text.Encoding]::UTF8

105 [C:\Users\leeholm]
>> $r1

Encoding                                          FormatProvider
--------                                          --------------
System.Text.SBCSCodePageEncoding                  en-US