#---------------------------------------------------------------- # Md5.ps1 #---------------------------------------------------------------- param ( [string]$filespec = $null, [bool]$colons = $false ); #---------------------------------------------------------------- # function Do-Md5 #---------------------------------------------------------------- function Do-Md5() { param ( [string]$filespec = $null, [bool]$colons = $false ); $hashAlgorithm = new-object -TypeName "System.Security.Cryptography.MD5CryptoServiceProvider"; $files = @(Get-ChildItem -Path $filespec -ErrorAction SilentlyContinue); foreach ($file in $files) { $stream = $file.OpenRead(); $hashByteArray = $hashAlgorithm.ComputeHash($stream); $stream.Close(); $md5StringBuilder = New-Object System.Text.StringBuilder $hashByteArray | % { if ( $colons ) { if ( $md5StringBuilder.Length -gt 0 ) { [void] $md5StringBuilder.Append(":") } } [void] $md5StringBuilder.Append($_.ToString("x2")) } "MD5($($file.Name))= $($md5StringBuilder.ToString())"; # Ensure stream is closed if exceptions occurred. trap { if ($stream -ne $null) { $stream.Close(); } break; } } } Do-Md5 -filespec $filespec -colons $colons;