I'm very new to Powershell, but am attempting to write a simple function to compare two files using their hashes. I'm getting some unexpected results using the following :
$hash1 = Get-FileHash $source | Select-Object Hash
Write-Host(" hash1 : " + $hash1)
returns : hash1 : @{Hash=93725215281E09E21317EA88E03B246AE13890ED96BB0B842A05A5E4969A4BFA}
$hash2 = Get-FileHash $destination | Select-Object Hash
Write-Host(" hash2 : " + $hash2)
returns : hash2 : @{Hash=93725215281E09E21317EA88E03B246AE13890ED96BB0B842A05A5E4969A4BFA}
$hashdiff = ($hash1 -eq $hash2)
Write-Host(" hashdiff : " + $hashdiff)
returns : hashdiff : False
I'm sure it's an obvious mistake, but can somebody put me out of my misery and help me understand why the equality operator isn't working as expected?
Many thanks in advance
The lines
Get-FileHash $source | Select-Object Hash
(same for$destination
) return PSObjects containing a propertyHash
.It's that property you want to compare, so either do
Or get the hash string values and compare those:
Result: