Read UTF-8 files correctly with PowerShell

后端 未结 3 669
慢半拍i
慢半拍i 2020-12-29 09:58

Following situation:

  • A PowerShell script creates a file with UTF-8 encoding
  • The user may or may not edit the file, possibly losing the BOM, but should
3条回答
  •  旧时难觅i
    2020-12-29 10:17

    Get-Content doesn't seem to handle UTF-files without BOM at all (if you omit the Encoding-flag). System.IO.File.ReadLines seems to be an alternative, examples:

    PS C:\temp\powershellutf8> $a = Get-Content .\utf8wobom.txt
    PS C:\temp\powershellutf8> $b = Get-Content .\utf8wbom.txt
    PS C:\temp\powershellutf8> $a2 = Get-Content .\utf8wbom.txt -Encoding UTF8
    PS C:\temp\powershellutf8> $a
    ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖ  <== This doesnt seem to be right at all
    PS C:\temp\powershellutf8> $b
    ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖ
    PS C:\temp\powershellutf8> $a2
    ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖ
    PS C:\temp\powershellutf8>
    PS C:\temp\powershellutf8> $c = [IO.File]::ReadLines('.\utf8wbom.txt');
    PS C:\temp\powershellutf8> $c
    ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖ
    PS C:\temp\powershellutf8> $d = [IO.File]::ReadLines('.\utf8wobom.txt');
    PS C:\temp\powershellutf8> $d
    ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖ <== Works!
    

提交回复
热议问题