tl;dr个
不幸的是,解决方案(在Windows上)比人们希望的要复杂得多:
# Make PowerShell both send and receive data as UTF-8 when talking to
# external (native) programs.
# Note:
# * In *PowerShell (Core) 7+*, $OutputEncoding *defaults* to UTF-8.
# * You may want to save and restore the original settings.
$OutputEncoding = [Console]::OutputEncoding = [Text.UTF8Encoding]::new()
# Create a BOM-less UTF-8 file.
# Note: In *PowerShell (Core) 7+*, you can less obscurely use:
# ruby -E UTF-8 -e "puts 'どうぞよろしくお願いします,Mr Jason'" | Set-Content test.txt
$null = New-Item -Force test.txt -Value (
ruby -E UTF-8 -e "puts 'どうぞよろしくお願いします,Mr Jason'"
)
# Pipe the resulting file back to Ruby as UTF-8, thanks to $OutputEncoding
# Note that PowerShell has NO "<" operator - stdin input must be provided
# via the pipeline.
Get-Content -Raw test.txt | ruby -E UTF-8 -e "puts gets"
就character encoding而言,PowerShell通过包含.NET System.Text.Encoding
实例的两个设置与外部(本机)程序通信:
从PowerShell 7.3.1开始,PowerShell只在与外部程序通信时"说出文本",而在intermediate decoding and re-encoding step is invariably involved版本中--即使你只是在使用>
(实际上是Out-File
个cmdlet的别名)将输出发送到文件时也是如此.
也就是说,PowerShell的管道不像其他shell 中的那样raw byte conduits%.
无论您使用什么输出运算符(>
)或cmdlet(Out-File
,Set-Content
),都将使用its默认字符编码,这与原始输入的编码无关,当运算符/cmdlet对其进行操作时,将使用has already been decoded into .NET strings.
Windows PowerShell中的>
/Out-File
默认为"Unicode"(UTF-16LE)编码,这就是您所看到的.
虽然Out-File
和Set-Content
有一个-Encoding
参数,允许您控制输出编码,但在Windows PowerShell中,它们不允许您创建BOM-less个UTF-8文件;奇怪的是,New-Item
does创建这样的文件,这就是上面使用它的原因;如果UTF-8 BOM是可以接受的,那么... | Set-Content -Encoding utf8
在Windows PowerShell中就可以了.
请注意,相比之下,PowerShell (Core) 7+版现在幸运的是,现代的跨平台版本一直默认使用无BOM的UTF-8.
- 尽管如此,对于Windows上的
[Console]::OutputEncoding
,从v7.3.1开始,它仍然默认使用传统的OEM代码页,这意味着来自外部程序的UTF-8输出默认为misinterpreted-请参阅GitHub issue #7233以了解相关讨论.