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

前端 未结 13 1352
名媛妹妹
名媛妹妹 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:17

    If you want to use [System.IO.File]::WriteAllLines(), you should cast second parameter to String[] (if the type of $MyFile is Object[]), and also specify absolute path with $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($MyPath), like:

    $Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False
    Get-ChildItem | ConvertTo-Csv | Set-Variable MyFile
    [System.IO.File]::WriteAllLines($ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($MyPath), [String[]]$MyFile, $Utf8NoBomEncoding)
    

    If you want to use [System.IO.File]::WriteAllText(), sometimes you should pipe the second parameter into | Out-String | to add CRLFs to the end of each line explictly (Especially when you use them with ConvertTo-Csv):

    $Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False
    Get-ChildItem | ConvertTo-Csv | Out-String | Set-Variable tmp
    [System.IO.File]::WriteAllText("/absolute/path/to/foobar.csv", $tmp, $Utf8NoBomEncoding)
    

    Or you can use [Text.Encoding]::UTF8.GetBytes() with Set-Content -Encoding Byte:

    $Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False
    Get-ChildItem | ConvertTo-Csv | Out-String | % { [Text.Encoding]::UTF8.GetBytes($_) } | Set-Content -Encoding Byte -Path "/absolute/path/to/foobar.csv"
    

    see: How to write result of ConvertTo-Csv to a file in UTF-8 without BOM

提交回复
热议问题