rohabu
2/19/2014 - 9:56 PM

Calculate MD5 Hash

Calculates the md5 hash of files in a folder (filter and exclude can be assigned) and writes it to an output file

$md5 = [Security.Cryptography.MD5]::Create()
Get-ChildItem -Path X:\temp\*.zip -Exclude *.md5 |
Where-Object -FilterScript {
  $_ -is [IO.FileInfo] 
} |
ForEach-Object -Process {
  $fstream = New-Object -TypeName System.IO.FileStream -ArgumentList ($_.FullName, [IO.FileMode]::Open, [IO.FileAccess]::Read)
  $md5byte = $md5.ComputeHash($fstream)
  $fstream.Close()
  $sb = New-Object -TypeName System.Text.StringBuilder
  for ($i = 0; $i -lt $md5byte.Length; $i++) 
  {
    $null = $sb.Append('{0:X2}' -f $md5byte[$i]) 
  }
  $("{0}`t{1}" -f $_.FullName, $sb.ToString().ToLower())
} |
Add-Content -Path X:\MD5Result\MD5Hash.txt