Simple PowerShell MD-5 Script
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); }
About this entry
You’re currently reading “Simple PowerShell MD-5 Script,” an entry on number8wire.com
- Published:
- 08 Dec 10 / 4 PM
- Category:
- Richard's Blog / Work
- Tagged:
Comments are closed
Comments are currently closed on this entry.