Question Script to recursively run MD5 checksum on files in a folder and subfolders

sirhawkeye64

Distinguished
May 28, 2015
134
0
18,690
Is there a PowerShell script I can run that will recursively run an MD5 checksum on files within a folder structure and store them into a text file (CSV)?

I have a computer that syncs to the cloud but my concern is that a file might become corrupt on the local machine and then be sent to the cloud (i do have backups of my data) but I'm looking for a way to initially create an MD5 checksum when the file is first copied to the machine after being verified, and then use it to check in in the future - possibly with automation -- to help detect files that are corrupt so they are not synced to the cloud).

Or if there is a program/product that can do this for me, please let me know. I know MD5 is not widely used anymore since it's been cracked and that the SHA method is used, but for my uses, I'm not necessarily concerned with tampering via outside sources, just corruption within my own ecosystem due to things like bitrot.
 
This command will check an entire folder for you. You would run the command for both folders.

Code:
Get-ChildItem -Path "<folder_path>" -Recurse -File | Get-FileHash -Algorithm <algorithm_name>

If you pipe this command to the end of the command above it will output into a csv file. At which point you could compare the hashes on the two different systems. To pipe you use the "|" which is on the \ key.

Code:
Export-Csv -Path '<full path to save the file and its name>' -NoTypeInformation

I'm not good enough at scripting to have the two files checked automatically but I bet someone else would know how to do that.
 
  • Like
Reactions: Ralston18
@sirhawkeye64

You can create a list of the folders to be checked and use a loop to read each folder name from the list and and process the folder per Get-ChildItem and Get-FileHash cmdlets.

There are a number of ways to do so with respect to Powershell's cmdlets. ForEach and ForEach-Object are likely candidates. The list being an array or collection.

I would create a system variable, assign a value to that variable from the folder name list, and then loop until all listed folder names have been completed.

One real advantage is that if you decide to add other folders to the scan you simply add the folder names to the list.

When using loops it is a good idea to set up some interim "writes" to display what is being done. Then change the writes to comments once the script is tested and proving to work as intended. Much easier to reinstate the writes again to troubleshoot future problems.