Skip to content

Instantly share code, notes, and snippets.

@cbilson
Created August 10, 2012 22:07
Show Gist options
  • Save cbilson/3318522 to your computer and use it in GitHub Desktop.
Save cbilson/3318522 to your computer and use it in GitHub Desktop.
BOM detector
function Is-BOM-File ($filename) {
$bytes = Get-Content -Encoding byte -TotalCount 4 $filename
$encoding = [Text.Encoding]::GetEncodings() `
| ForEach { $_.GetEncoding() } `
| Where-Object { -not $_.IsSingleByte } `
| Where-Object {
$preamble = $_.GetPreamble()
For ($i = 0; $i -lt $preamble.Length; $i++) {
if ($bytes[$i] -ne $preamble[$i]) {
return $false
}
$true
}
}
$encoding -ne $null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment