使用PowerShell在没有BOM的情况下以UTF-8编写文件 [英] Using PowerShell to write a file in UTF-8 without the BOM

查看:811
本文介绍了使用PowerShell在没有BOM的情况下以UTF-8编写文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Out-File 似乎强制BOM使用UTF-8时:

Out-File seems to force the BOM when using UTF-8:

$MyFile = Get-Content $MyPath
$MyFile | Out-File -Encoding "UTF8" $MyPath

如何使用UTF-8编写文件没有使用PowerShell的BOM?

How can I write a file in UTF-8 with no BOM using PowerShell?

推荐答案

使用.NET的 UTF8Encoding 类和$ $ False 到构造函数似乎工作:

Using .NET's UTF8Encoding class and passing $False to the constructor seems to work:

$MyFile = Get-Content $MyPath
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False
[System.IO.File]::WriteAllLines($MyPath, $MyFile, $Utf8NoBomEncoding)

这篇关于使用PowerShell在没有BOM的情况下以UTF-8编写文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆