Skip to content

Instantly share code, notes, and snippets.

@chrisbrownie
Last active January 11, 2022 04:24
Show Gist options
  • Save chrisbrownie/90adc050351c1d236ca23765874d54e2 to your computer and use it in GitHub Desktop.
Save chrisbrownie/90adc050351c1d236ca23765874d54e2 to your computer and use it in GitHub Desktop.
Compares a file to a given hash
function Compare-FileToHash {
<#
.SYNOPSIS
Compare-FileToHash - Compares a file to a given hash and returns a boolean of whether they match or not
.DESCRIPTION
This function will compare a file to a given hash (in string format) and
returns the boolean $true or $false depending on whether the hashes match.
Supported algorithms are all those supported by Get-FileHash.
.EXAMPLE
Compare-FileToHash -expectedHash F683E63A08F385F52E86990CEB62CF1374A23373 -algorithm SHA1 -Path $env:SYSTEMROOT\System32\shell32.dll
.LINK
https://flamingkeys.com/compare-file-hash-powershell/
.NOTES
Written by: Chris Brown
Find me on:
* My Blog: https://flamingkeys.com
* Twitter: https://twitter.com/chrisbrownie
* GitHub: https://github.com/chrisbrownie
#>
Param(
$expectedHash,
$algorithm="MD5",
$Path
)
$actualHash = (Get-FileHash -Path $Path -Algorithm $algorithm).Hash
New-Object -TypeName PSObject -Property @{
"Path" = $Path
"ExpectedHash" = $expectedHash
"ActualHash" = $actualHash
"Match" = if ($expectedHash -eq $actualHash) { $true } else { $false }
}
}
@chrisbrownie
Copy link
Author

I like it...all except the use of Write-Host, which I don't like! Thanks for the contribution :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment