1

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?

4
  • 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? Commented Jul 27, 2022 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). Commented Jul 27, 2022 at 12:18
  • @zett42 Interestingly enough, the hashes match when I copy within the same script. Commented Jul 27, 2022 at 12:18
  • 1
    Also, try: Get-FileHash -LiteralPath ... Commented Jul 27, 2022 at 12:21

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.