Powershell-编码UTF-8在Export-Csv中不起作用

时间:2019-04-25 16:17:39

标签: powershell encoding utf-8 export-csv

我正在尝试输出大量数据,并请一些德国的Umlauts:ä,ö,ü.. 我在Export-Csv的末尾添加了-Encoding UTF8,但实际上并没有用。 我的代码:

For ($i = 0; $i -lt $accFileCont.Length; $i++) {
        $accFileCont[$i] | Export-Csv $outAccFile -NoTypeInformation -Encoding UTF8
    }

我期望ä,ö,ü...而不是“ä”,... 怎么了它不输出Umlauts。 在此先感谢:)

1 个答案:

答案 0 :(得分:1)

对我来说,实际上是“-Encoding utf8BOM”完成了这项工作

环境: Windows 10 20H2 德语, 德文Excel版, PowerShell 7

如果我没有使用 utf8BOM,我的德语 Excel 没有正确显示“变音符号”。

There is a whole discussion about UTF8 with or without BOM

实际上是“马吕斯”wrote the following comment: “它可能不被推荐,但它在尝试输出“æøå”时对我的 powershell 脚本产生了奇迹”

“C:\sources\new_18.json”格式为 UTF-8(这是一个简单的 JSON 文件)

"-Delimiter ";"" 用于德语 Excel

    $TestVariable = (Get-Content -raw -Encoding UTF8 "C:\sources\new_18.json") | ConvertFrom-Json

    $TestVariable.value | Export-Csv "C:\sources\PSOutput\Output_$((Get-Date).ToString('MM-dd-yyyy_hh-mm-ss')).csv" -NoTypeInformation -Delimiter ";" -Encoding utf8BOM

对于 PowerShell 5: 将“-Encoding utf8BOM”更改为“-Encoding utf8” (在Powershell 5中“Export-Csv -Encoding utf8”将文档保存在utf8BOM中)