I write a Function in PowerShell script to compare the hash between two files. This is my code:
Function Compare-Hash {
param (
$FILE_1,
$FILE_2
)
$HASH_1 = Get-FileHash $FILE_1 -Algorithm SHA256 | Format-List Hash
$HASH_2 = Get-FileHash $FILE_2 -Algorithm SHA256 | Format-List Hash
$HASH_MATCHED = @(Compare-Object $HASH_1 $HASH_2).Lenght -eq 0
If ($HASH_MATCHED) {
Write-Host -ForegroundColor Green Hash matched!
}
else {
Write-Host -ForegroundColor Red Hash not matched!
}
}
I tried that Function with the same file:
Compare-Hash test.txt test.txt
But it returned:
Hash not matched!
I also try with
If ($HASH_1 -eq $HASH_2)
But it still returns false.
I am sticking with this problem and I don't know why this happened. I also print $HASH_1 and $HASH_2 to check it manually but those are the same!
Could you show me what my problem is and how to fix it? Thank you very much.