Same file, different hashes with PowerShell Get-FileHash command

843 Views Asked by At

I'm trying to write a PowerShell script that will copy image files from one folder to another, but only if the files don't already exist in the destination folder.
The files are randomly named.

The issue is that Get-FileHash keeps returning different hashes even when the files are identical.

For the sake of simplicity, I produced the following scenario:
I have 2 JPG files, which are the same image.

  • The file size is the same.
  • Running fc.exe /b image1.jpg .\temp\image2.jpg returns
    FC: no differences encountered

However, PowerShell gives them different hashes:

$oldFileHash = Get-FileHash .\image1.jpg  -Algorithm "SHA256"
>> $newFileHash = Get-FileHash .\temp\image2.jpg -Algorithm "SHA256"
>>
>> $oldFileHash.Hash
>> $newFileHash.Hash
21CE9E2CE18AC46DF13400C4CEFA11FB254D96E9D39BD67FA2F4189ACF4F5D3B
6441924D9D2349D3CFD8164B18DF8DA2FFA9F281DE198E56C0AE4CFDBFBCE8AD

I tried using MD5 algorithm instead of SHA256, and got the different hashes as well.

What would cause this behavior?

1

There are 1 best solutions below

0
On

As zett42 suggested, this must have been a test error.
I've deleted contents of both folders and started over, and my script worked as expected