June 7, 2009

Powershell Get MD5 For File

Often when comparing 2 files you have the following 3 options:
  • Date/Time Stamp
  • CRC Check
  • MD5 Check
Each one has a speed tag associated with it where date-time is fast and MD5 is a bit slower. MD5 will however give the best comparison check. Below is an example on how to get the MD5 hash associated with a file:
function Get-MD5File {
Param([string]$file)
$open = [System.IO.FileMode]("open")
$read = [System.IO.FileAccess]("Read")
$md5 = new-object System.Security.Cryptography.MD5CryptoServiceProvider
$fs = new-object System.IO.FileStream($file, $open, $read)
$Hash = $md5.ComputeHash($fs)
$fs.Close()
return $Hash
}

#test a file
$MD5File = Get-MD5File c:\test.pl
Write-Host $MD5File
I believe that the POSh community extensions located here has some easy way to do this as well.

No comments:

Post a Comment