Generating a MD5 Checksum With Powershell

Recently, I was comparing .wim file images used for PXE booting and installing Operating System Images using SCCM OSD.  I was found that I received a Winload.exe boot problem when trying to build devices off a remote server.  The .wim file it was using looked the correct size in bytes, but generating an MD5 checksum against it showed that the copy on the remote server was different from the source .wim file.

Please find the Powershell function below to generate an MD5 against a file.

Function Get-Hash($file){
 
    $algo = [System.Security.Cryptography.HashAlgorithm]::Create("MD5")
    $stream = New-Object System.IO.FileStream($file, [System.IO.FileMode]::Open)
 
    $md5StringBuilder = New-Object System.Text.StringBuilder
    $algo.ComputeHash($stream) | % { [void] $md5StringBuilder.Append($_.ToString("x2")) }
    $md5StringBuilder.ToString()
 
    $stream.Dispose()
}