Created
December 15, 2011 12:43
-
-
Save zommarin/1480974 to your computer and use it in GitHub Desktop.
Get-FileEncoding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-FileEncoding($Path) { | |
$bytes = [byte[]](Get-Content $Path -Encoding byte -ReadCount 4 -TotalCount 4) | |
if(!$bytes) { return 'utf8' } | |
switch -regex ('{0:x2}{1:x2}{2:x2}{3:x2}' -f $bytes[0],$bytes[1],$bytes[2],$bytes[3]) { | |
'^efbbbf' { return 'utf8' } | |
'^2b2f76' { return 'utf7' } | |
'^fffe' { return 'unicode' } | |
'^feff' { return 'bigendianunicode' } | |
'^0000feff' { return 'utf32' } | |
default { return 'ascii' } | |
} | |
} |
Thank-you for sharing; this is really helpful
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for your code, but it not always shows truth.
Got working sample: https://github.com/mac2000/WindowsPowerShell/blob/master/Modules/Is-UTF8/Is-UTF8.psm1
Idea from: https://github.com/madx/moreutils/blob/master/isutf8.c
Downside that it will work only with small files.