I’m going to start posting a few useful powershell scripts. I’m not sure where I picked up this snippet but it’s pretty useful. I modified it to resolve-path the param so that you can pass in relative paths.
function Get-MD5([System.IO.FileInfo] $file = $(throw 'Usage: Get-MD5 [System.IO.FileInfo]'))
{
$file = [System.IO.FileInfo] (Resolve-Path $file).Path
$stream = $null;
$cryptoServiceProvider = [System.Security.Cryptography.MD5CryptoServiceProvider];
$hashAlgorithm = new-object $cryptoServiceProvider
$stream = $file.OpenRead();
$hashByteArray = $hashAlgorithm.ComputeHash($stream);
$stream.Close();
## We have to be sure that we close the file stream if any exceptions are thrown.
trap
{
if ($stream -ne $null)
{
$stream.Close();
}
break;
}
return [System.Convert]::ToBase64String($hashByteArray);
}