Powershell: Setting Encoding for Get-Content Pipeline

后端 未结 4 1637
说谎
说谎 2020-12-14 06:07

I have a file saved as UCS-2 Little Endian I want to change the encoding so I ran the following code:

cat tmp.log -encoding UTF8 > new.log
相关标签:
4条回答
  • 2020-12-14 06:48

    As suggested here:

    Get-Content tmp.log | Out-File -Encoding UTF8 new.log
    
    0 讨论(0)
  • 2020-12-14 06:50

    If you are reading an XML file, here's an even better way that adapts to the encoding of your XML file:

    $xml = New-Object -Typename XML
    $xml.load('foo.xml')
    
    0 讨论(0)
  • 2020-12-14 06:51

    load content from xml file with encoding.

    (Get-Content -Encoding UTF8 $fileName)

    0 讨论(0)
  • 2020-12-14 06:52

    I would do it like this:

    get-content tmp.log -encoding Unicode | set-content new.log -encoding UTF8
    

    My understanding is that the -encoding option selects the encdoing that the file should be read or written in.

    0 讨论(0)
提交回复
热议问题