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?.
Asked
Active
Viewed 85 times
0
-
see [this](https://stackoverflow.com/questions/5596982/using-powershell-to-write-a-file-in-utf-8-without-the-bom) – Sep 06 '17 at 11:08
-
I read it is not helpful,thank you for your help – Amr Hassan Sep 06 '17 at 11:13
-
The linked question and its answers are about writing a file with a particular encoding, not about detecting the encoding of an existing file. – Ansgar Wiechers Sep 06 '17 at 11:32
-
[Related](https://stackoverflow.com/q/33176058/1630171). – Ansgar Wiechers Sep 06 '17 at 11:33
1 Answers
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
- 2,349
- 1
- 12
- 24
-
-
-
it is not working,I have test several times using different formats ,and only showing ASCII,your answer didn't achieve the purpose,anyway thanks – Amr Hassan Sep 13 '17 at 14:59