Powershell:为Get-Content管道设置编码

jed*_*atu 45 powershell encoding

我有一个文件保存为UCS-2 Little Endian我想更改编码,所以我运行以下代码:

cat tmp.log -encoding UTF8 > new.log
Run Code Online (Sandbox Code Playgroud)

生成的文件仍在UCS-2 Little Endian中.这是因为管道始终采用这种格式吗?有没有一种简单的方法可以将其作为UTF8传输到新文件?

Lar*_*ens 54

正如这里建议的:

Get-Content tmp.log | Out-File -Encoding UTF8 new.log
Run Code Online (Sandbox Code Playgroud)

  • 请参阅http://stackoverflow.com/questions/5596982/using-powershell-to-write-a-file-in-utf-8-without-the-bom (2认同)

dri*_*iis 28

我会这样做:

get-content tmp.log -encoding Unicode | set-content new.log -encoding UTF8
Run Code Online (Sandbox Code Playgroud)

我的理解是-encoding选项选择应该读取或写入文件的encdoing.

  • "Get-Help Set-Content"不会将-encoding标识为一个选项,但它实际上确实有效. (10认同)
  • @jedatu:那是因为这个参数来自FileSystem提供者. (8认同)

小智 7

使用编码从xml文件加载内容.

(Get-Content -Encoding UTF8 $ fileName)