0

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?

Alon_EH
  • 11
  • 2
  • This looks like a test error. Can you make this reproducable, e. g. `Copy-Item image1.jpg temp\image2.jpg` and then run `fc.exe` and `Get-FileHash` from within the same script? – zett42 Jul 27 '22 at 10:55
  • 2
    Agree with @zett42. Note that `.\image1.jpg` and `.\temp\image2.jpg` are relative paths. Are you looking to the same paths as `fc.exe`? (try using absolute paths). – iRon Jul 27 '22 at 12:18
  • @zett42 Interestingly enough, the hashes match when I copy within the same script. – Alon_EH Jul 27 '22 at 12:18
  • 1
    Also, try: [`Get-FileHash -LiteralPath ...`](https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/get-filehash) – iRon Jul 27 '22 at 12:21

1 Answers1

1

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

Alon_EH
  • 11
  • 2