How to compare all files in two folders using PowerShell

When I am investigating or troubleshooting any issue, I always hope to have access to any log,  source control, build artifacts, and pipelines. However, some scenarios are not related to software development where the only resources that I have immediate access to are folders and the files contained, and also the same ones but from a daily backup.

What happened to me at least twice was the need of at glance of compare files and folders by using hashes and relative file paths to identify missing or new files.

Both times I wrote something on the spot, it was rough but it did the job. It gave me the answer that I was looking for and I knew that the folders and their content were exactly the same.

This last time when I was asked if I had something ready for this I thought that writing an article to share it could be beneficial for the whole community.

Outcomes

Given 2 distinct folders I wanted to analyse the relative path and the hash of each of the files to identify if the content was identical or not.  If the latter knowing if the file was added, missing or changed is also equally important.

Compare-Folder PowerShell script

Conclusions

When you need to test your assumptions simple tools may help you to gather some clues or preliminary information that help you move quickly to find the root cause.

This script is very basic, but the outcome of comparing the content of two folders recursively is more powerful than it seems.

I hope you will find it useful! As usual, you can find this script on my Github repository.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.