Compare a file to a hash with PowerShell
Written on May 13, 2017

This simple function allows a caller to compare a file to an existing hash in any format supported by Get-FileHash. These, at time of writing (PowerShell 5.1 build 14393), are:

Using the script is quite simple. Add it to your console or a script, then run:

Compare-FileToHash -Path "C:\windows\system32\shell32.dll" -expectedHash "F683E63A08F385F52E86990CEB62CF1374A23373" -algorithm "SHA1"

This will return the full object that shows the expected, generated, and hashing result:

Match Path                            ActualHash                               ExpectedHash
----- ----                            ----------                               ------------
 True C:\windows\system32\shell32.dll F683E63A08F385F52E86990CEB62CF1374A23373 F683E63A08F385F52E86990CEB62CF1374A23373

The result only can be retrieved by running the script as follows:

(Compare-FileToHash -Path "C:\windows\system32\shell32.dll" -expectedHash "F683E63A08F385F52E86990CEB62CF1374A23373" -algorithm "SHA1").Match

This will return a boolean, either $true or $false.

True

Easy!

Comments/questions

There's no commenting functionality here. If you'd like to comment, please either mention me (@chris@aus.social) on Mastodon or email me. I don't have any logging or analytics running on this website, so if you found something useful or interesting it would mean a lot to hear from you.