Powershell: Setting Encoding for Get-Content Pipel

2019-02-02 02:04发布

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

The resulting file is still in UCS-2 Little Endian. Is this because the pipeline is always in that format? Is there an easy way to pipe this to a new file as UTF8?

4条回答
趁早两清
2楼-- · 2019-02-02 02:40

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')
查看更多
再贱就再见
3楼-- · 2019-02-02 02:54

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.

查看更多
一纸荒年 Trace。
4楼-- · 2019-02-02 02:54

load content from xml file with encoding.

(Get-Content -Encoding UTF8 $fileName)

查看更多
做自己的国王
5楼-- · 2019-02-02 03:03

As suggested here:

Get-Content tmp.log | Out-File -Encoding UTF8 new.log
查看更多
登录 后发表回答