PowerShell中的UTF8脚本输出不正确的字符 [英] UTF8 Script in PowerShell outputs incorrect characters
问题描述
我为具有非ASCII字符的PowerShell创建了UTF8脚本.
I've created a UTF8 script for PowerShell with non-ascii characters.
characters.ps1:
characters.ps1:
Write-Host "ç â ã á à"
在PowerShell控制台中运行脚本时,它会输出错误的字符.
When the script is run in PowerShell console, it outputs wrong characters.
但是,如果我直接在控制台中编写字符,它们将按预期显示:
However, if I write the chars directly in the console, they are shown as expected:
有人知道导致这种行为的原因吗?
Does anyone knows what causes that behavior?
问题是由我编写的脚本引起的,该脚本具有包含非ASCII字符的硬编码路径.当我尝试将路径作为参数传递给cmdlet时(如果我要自动复制一个文件夹),该命令将失败,因为它找不到路径(在屏幕上错误输出).
The problem arised from a script I wrote who has hardcoded paths which include non-ascii characters. When I try to pass the path as argument to cmdlets (in the case I was gonna robocopy a folder) the command fails because it cannot find the path (which is output wrongly in the screen).
推荐答案
使用BOM将脚本的编码更改为UTF-8解决了该问题.
我正在将SublimeText与EncodingHelper插件一起使用来控制脚本的字符集.它已正确设置为UTF8.
I was using SublimeText with the EncodingHelper plugin to control the character-set of the script. It was set correctly to UTF8.
我将SublimeText中脚本的编码更改为带BOM的UTF-8",并且输出显示正确.
I changed the encoding of the script in SublimeText to "UTF-8 with BOM" and the output was shown correctly.
我使用Notepad ++创建了相同的脚本,默认为带BOM的UTF-8",并且该字符串已在控制台中正确显示.
I created the same script with Notepad++, which defaults to "UTF-8 with BOM", and the string was shown correctly in the console.
我在Notepad ++中将脚本的编码更改为不带BOM的UTF-8",并且显示不正确.
I changed the encoding of the script in Notepad++ to "UTF-8 without BOM" and it was shown incorrectly.
似乎PowerShell无法正确猜测没有BOM的UTF-8文件的编码.
这篇关于PowerShell中的UTF8脚本输出不正确的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!