powershellencodingutf-8byte-order-mark

Remove BOM from UTF 8 using cmd


I need to remove BOM using cmd file from MyFile.txt. The file is located here.

| Out-File -encoding utf8 '%CD%\MyFile.txt'

I need it to be removed using only one cmd, that is, in the next lines. I need it to be backwards compatible to Windows 7. If I needed it for myself only I would just use -encoding default, but its not backwards compatible even to win 10. Just one file. There were many different questions about BOM on different situations, my issue is I need to use one .cmd, I already have utf8 with BOM and I need it without BOM. Please help me.

I was trying to use powershell but the issue is powershell syntax is not really compatible to cmd? It just says about unrecognised syntax everytime I tried anything from the very popular theme like this Using PowerShell to write a file in UTF-8 without the BOM.


Solution

  • PowerShell is indeed your best bet, and while you cannot directly use PowerShell commands from cmd.exe / a batch file, you can pass them to powershell.exe, the Windows PowerShell CLI (the solutions below also work on the no longer supported Windows 7 edition of Windows, as requested).

    Here's a sample batch file that demonstrates a solution:

    @echo off & setlocal
    
    :: Specify the input file, assumed to be a UTF-8 with-BOM file.
    set "targetFile=%CD%\MyFile.txt"
    
    :: Call Windows PowerShell in order to 
    :: convert the file to a *BOM-less** UTF-8 file.
    powershell -noprofile -c $null = New-Item $env:targetFile -Force -Value (Get-Content -Raw -LiteralPath $env:targetFile)
    

    Note:

    The following solution addresses these problems:

    @echo off & setlocal
    
    set "targetFile=%CD%\MyFile.txt"
    
    powershell -noprofile -c $ErrorActionPreference='Stop'; $tempFile=New-TemporaryFile; $inFile=Convert-Path -LiteralPath $env:targetFile; [IO.File]::WriteAllLines($tempFile, [IO.File]::ReadLines($inFile)); $tempFile ^| Move-Item -Force -Destination $inFile
    

    Note: