Cheeky PowerShell Hash Script

Random

If you want to check the (MD5/SHA) hash of a file you can do this on Microsoft Windows with “Get-FileHash” within PowerShell.

Here is a simple script to take a File, the Hash Algorithm Type and Expected Hash for the file to compare, it hashes the file and then compares it with the expected hash for the file, if it matches or not it reports this.

param($FileName,$AlgoType,$CompareHash)

$hash = Get-FileHash $Filename -algorithm $AlgoType

$InputHash = $hash.hash

Write-Host "Input Filename:" $FileName
Write-Host "Input File Hash:" $InputHash.ToLower() " Expected Hash:" $CompareHash" = " -NoNewline

if ($InputHash -eq $CompareHash) {
    Write-Host "Hash Matches!" -ForegroundColor Green
} else {
    Write-Host "No Hash Match!" -ForegroundColor Red
}

For example, i’ve downloaded an XOS image for an Extreme Networks switch, if we run the following with the expected hash:

.\md5sum.ps1 -FileName '.\summitX-22.7.5.1-patch1-3.xos' -AlgoType MD5 -CompareHash 34600b5ce116c26841ce8537d2ded620

We get the following:

Input Filename: .\summitX-22.7.5.1-patch1-3.xos
Input File Hash: 34600b5ce116c26841ce8537d2ded620  Expected Hash: 34600b5ce116c26841ce8537d2ded620 = Hash Matches!

If the file does not hash to the expected value we get:

Input Filename: .\summitX-22.7.5.1 (1).xos
Input File Hash: d35b34b993f9c5e91aaa83d547d13a11  Expected Hash: 34600b5ce116c26841ce8537d2ded620 = No Hash Match!

Leave a Reply

Your email address will not be published. Required fields are marked *