$filePath='C:\Windows\System32\Drivers\etc\hosts'
function getFileEncoding($filePath) {
# Checking the byte order mark (BOM) or first 4 bytes
$bom = [byte[]](Get-Content $filePath -Encoding byte -ReadCount 4 -TotalCount 4)
if(!$bom) { # case: UTF-8 without BOM
return 'utf8'
}else{
switch -regex ('{0:x2}{1:x2}{2:x2}{3:x2}' -f $bom[0],$bom[1],$bom[2],$bom[3]) {
'^2b2f76' { return 'UTF-7' } # endianless
'^efbbbf' { return 'UTF-8' } # endianless
'^fffe' { return 'UTF-16-LE' } #little endian
'^feff' { return 'UTF-16-BE' } #big endian
'^fffe0000' { return 'UTF-32-LE' } #little endian
'^0000feff' { return 'UTF-32-BE' } #big endian
default { return 'ASCII' }
}
}
}
getFileEncoding $filePath
Categories: