In this post, I describe to verify .md5 and .SHA512 hashes with PowerShell, without any additional tools.

Checksums (like MD5 and SHA512) are typically used to verify the integrity of large downloads. This is especially common in open source software projects. On Linux, you can verify them via command line tools like md5sum or sha1sum. Windows doesn’t ship with these. However, PowerShell has the Get-FileHash commandlet. However, that doesn’t support the prevalent .md5 or .sha512 files as input. Hence, I wrote the following little function to rectify that:

function Verify-Hash{
    param
    (
        [Parameter(Mandatory=$true, HelpMessage='The file containing the hash.')]
        [string]$File,
        [Parameter(Mandatory=$true, HelpMessage='The Algorithm to use')]
        [string]$Algorithm
    ) 
    foreach ($line in (Get-Content $File)) {
        $fields = $line -split '\s+'
        $hash = $fields[0].Trim().ToUpper()
        $filename = $fields[1].Trim()
        if($filename.StartsWith("*")){
            $filename = $filename.Substring(1).Trim()
        }

        $computedHash = (Get-FileHash -Algorithm $Algorithm $filename).Hash.ToUpper()

        if($hash.Equals($computedHash)){
            Write-Host $filename, ": Passed"
        }else{
            Write-Host $filename, ": Not Passed"
            Write-Host "Read from file: ", $hash
            Write-Host "Computed:       ", $computedHash
        }
    }
}

To make things even easier, you can define functions for MD5:

function Verify-MD5{
    param
    (
        [Parameter(Position=0, Mandatory=$true, HelpMessage='The .MD5 file.')]
        [string]$File

    ) 
    Verify-Hash -Algorithm MD5 -File $File
}

and SHA512:

function Verify-SHA512{
    param
    (
        [Parameter(Position=0, Mandatory=$true, HelpMessage='The .SHA512 file.')]
        [string]$File

    ) 
    Verify-Hash -Algorithm SHA512 -File $File
}