Created
September 7, 2017 13:28
-
-
Save kevinobee/8a7ae210ca1b7cd4d19f879a0171265e to your computer and use it in GitHub Desktop.
Get-FileEncoding
This file contains hidden or 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 | |
{ | |
[CmdletBinding()] Param ( | |
[Parameter(Mandatory = $True, ValueFromPipelineByPropertyName = $True)] [string]$Path | |
) | |
[byte[]]$byte = get-content -Encoding byte -ReadCount 4 -TotalCount 4 -Path $Path | |
if ( $byte[0] -eq 0xef -and $byte[1] -eq 0xbb -and $byte[2] -eq 0xbf ) | |
{ Write-Output 'UTF8' } | |
elseif ($byte[0] -eq 0xfe -and $byte[1] -eq 0xff) | |
{ Write-Output 'Unicode' } | |
elseif ($byte[0] -eq 0 -and $byte[1] -eq 0 -and $byte[2] -eq 0xfe -and $byte[3] -eq 0xff) | |
{ Write-Output 'UTF32' } | |
elseif ($byte[0] -eq 0x2b -and $byte[1] -eq 0x2f -and $byte[2] -eq 0x76) | |
{ Write-Output 'UTF7'} | |
else | |
{ Write-Output 'ASCII' } | |
} | |
# Usage: | |
# Get-ChildItem . -Recurse -File | select FullName, @{n='Encoding';e={Get-FileEncoding $_.FullName}} # | where {$_.Encoding -ne 'ASCII'} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment