Last active
May 1, 2024 07:37
-
-
Save neuralpain/8a5b00a260bec6d1c12b6692005ff508 to your computer and use it in GitHub Desktop.
PowerShell: Get hash from file and compare against the original
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Compare-IsValidHash { | |
<# | |
.SYNOPSIS | |
Compares the file hash (MD5) of the specified file against the provided hash. | |
.DESCRIPTION | |
This function uses PowerShell's Get-FileHash cmdlet to retrieve the hash of the specified file. | |
It then compares the retrieved hash against the provided hash. If the two hashes match, the function returns $true, | |
otherwise it returns $false. | |
.PARAMETER Hash | |
A string containing the expected MD5 hash value | |
.PARAMETER File | |
A string containing the path to the file to be compared | |
.EXAMPLE | |
Compare-IsValidHash -Hash "1234567890ABCDEF" -File "C:\path\to\file.txt" | |
If the MD5 hash of "C:\path\to\file.txt" is "1234567890ABCDEF", this function will return $true. Otherwise it will return $false. | |
.OUTPUTS | |
Boolean | |
#> | |
[CmdletBinding()] | |
param ($Hash, $File) | |
# retrieve the hash of the specified file | |
$_hash = ((Get-FileHash $File -Algorithm MD5 | | |
Select-Object Hash) -split " ").Trim("@{Hash=}") | |
# compare the retrieved hash against the provided hash | |
if ($_hash -ne $Hash) { return $false } | |
else { return $true } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If using with batch, return exit code
1
to catch errors when powershell endsIn batch: