Every now and then you need to be able to programmatically determine a file's encoding. Maybe you are writing a utility that rewrites files and you want ensure you maintain the original type. Perhaps you want to make sure that certain files have a Byte Order Marker (BOM).
If the file has a BOM, this is easy. If it doesn't... aw, crap. At that point you have to analyze the file's contents and make a judgement call based on what you see. I wrote a function to do this: Get-DTWFileEncoding
Get-DTWFileEncoding returns a System.Text.Encoding type based on the file specified.
It will also return the appropriate encoding type based on the whether or not the original file had a BOM or not.
The included sample test script will run it against the sample files:
You can download a module containing the Get-DTWFileEncoding function, along with some sample files with different encodings, here. Check out the function help and the source if you want more information on the detection algorithm.