Using PowerShell to write a file in UTF-8 without the BOM

前端 未结 13 1453
名媛妹妹
名媛妹妹 2020-11-22 06:25

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

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

13条回答
  •  梦谈多话
    2020-11-22 07:11

    Change multiple files by extension to UTF-8 without BOM:

    $Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding($False)
    foreach($i in ls -recurse -filter "*.java") {
        $MyFile = Get-Content $i.fullname 
        [System.IO.File]::WriteAllLines($i.fullname, $MyFile, $Utf8NoBomEncoding)
    }
    

提交回复
热议问题