Adding a BOM to a UTF8 file with PowerShell

If your file is UTF8 encoded but misses the 3 Byte-Order-Marks (BOM) (see http://en.wikipedia.org/wiki/Byte_order_mark for details) you will encounter problems when you process with PowerShell and other utilities (ÆÅØ will get destroyed etc).

In other words, if Notepad++ shows encoding “ANSI as UTF8”, and your content gets corrupted, you need to insert 3 bytes in the beginning of the file:

function InsertBOMIntoDKOFile($rigFullDirName) {
[byte[]]$byte = get-content -Encoding byte -ReadCount 4 -TotalCount 4 -Path "$FileName"
if ( $byte[0] -eq 0xef -and $byte[1] -eq 0xbb -and $byte[2] -eq 0xbf )
 # UTF8 - we're done
 else {
 $bytes = [System.IO.File]::ReadAllBytes("$FileName")
 $byte = [Byte[]] (0xEF,0xBB,0xBF)
 set-content -value $byte -encoding byte -path "$FileName"
 add-content -value $bytes -encoding byte -path "$FileName"
 }
}

Skriv et svar

Udfyld dine oplysninger nedenfor eller klik på et ikon for at logge ind:

WordPress.com Logo

Du kommenterer med din WordPress.com konto. Log Out / Skift )

Twitter picture

Du kommenterer med din Twitter konto. Log Out / Skift )

Facebook photo

Du kommenterer med din Facebook konto. Log Out / Skift )

Google+ photo

Du kommenterer med din Google+ konto. Log Out / Skift )

Connecting to %s