0

I am new in PowerShell and I have a text file and I want to know the encoding of it using power shell scripting,how could I do it?.

Dave Sexton
  • 9,676
  • 3
  • 37
  • 51
Amr Hassan
  • 11
  • 1
  • 7

1 Answers1

0

Here is a code snipped, which I found with google. Try google next time before you ask!

Code example:

$FilePath = "C:\temp\new.txt"
[byte[]]$byte = get-content -Encoding byte -ReadCount 4 -TotalCount 4 -Path $FilePath
if ($byte -ne $null) {
    if ($byte.Count -ge 4) {
        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'
        }
    } else {
        Write-Error -Message ($Path + " is only " + $byte.Count + " bytes in size, unable to determine file encoding") -Category InvalidData
    }
} else {
    Write-Error -Message ($Path + " is zero byte(s) in size") -Category InvalidData
}

Found on the site: http://sushihangover.blogspot.ch/2012/10/powershell-file-encoding-checking-and.html

guiwhatsthat
  • 1,953
  • 1
  • 8
  • 18