Last active
January 11, 2022 04:24
-
-
Save chrisbrownie/90adc050351c1d236ca23765874d54e2 to your computer and use it in GitHub Desktop.
Compares a file to a given hash
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-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 } | |
} | |
} |
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
Awesome little snippet tweaked it a bit, to keep items in order and also add a default
-simple
that just does true / false