Posted On December 2, 2020

PowerShell: Check File Encoding UTF7, UTF8, UTF16, UTF32, ASCII

kimconnect 0 comments
blog.KimConnect.com >> Windows >> PowerShell: Check File Encoding UTF7, UTF8, UTF16, UTF32, ASCII
$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

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

How to Create a New Jenkins Job to Call a PowerShell Script with Credentials

1. PowerShell & Secured Credentials Support Check for PowerShell support: Manage Jenkins > Plugin Manager…

Error: Windows cannot load extensible counter DLL MSSQLSERVER, the first DWORD in data section is the Windows error code.

 Resolution:   Give everyone read/execute access to binn\sqlctr80.dll or msmdctr90.dll   # Set file$sqlctr =…

PowerShell: Detecting Windows Antivirus

One of the initial tasks of a Windows user is to determine whether a computer…