Product and service reviews are conducted independently by our editorial team, but we sometimes make money when you click on links. Learn more.
 

How To Automate File Hash Check With PowerShell

How To Automate File Hash Check With PowerShell
By

File hash checking allows you to ensure that the files you copy are 100 percent the same. When copying hundreds or thousands of files, you can automate file hash check with PowerShell -- here's how.

In Windows, copying files to a remote location can be as simple as dragging and dropping. Select one or more files in a Windows Explorer window, and drag them over to a destination file share. Simply wait for the files to stop copying, and you're done. Easy enough. But what if you need to copy over some critical files, and you need to be 100 percent sure the files were not modified in any way as they traversed the network to get there? The answer is file hash checking!

In simple terms, a file hash is a unique string of characters assigned to a file on your computer. This hash is calculated based on the contents of the file; not the file itself. For example, if a hash is calculated on a text file, and someone edits even a single character in this text file, the hash will be different when it is calculated again. This works well when moving files around, especially over a network. The protocols used to transfer data over the network on a Windows computer are reliable, but not fool-proof. 

To ensure a file is not modified during transfer, check the hash of the file before and after it is copied to the destination. There are a few ways to get this done.

The method I've always used with Windows Explorer is to employ Febooti Hash & CRC software. This software integrates into Windows Explorer and creates a special tab in the Properties menu for each file.To determine whether a file was modified during transfer, I'd simply do the following: right-click the file I want to copy at the source, compute an MD5 hash, take a screenshot, copy the file to the destination, and do the same again for each file. Then I'd eyeball the hash values both before and after the copy. This is an effective process if you're only copying a few files, but it definitely isn't if you're copying dozens or thousands of files!

So how would we handle this on a bigger scale? The answer is PowerShell.

I've developed a PowerShell function that handles all the hard work for you in doing these comparisons -- whether with a single file or 10,000 files. This script mimics exactly what I was doing the hard way in an automated fashion. Let me show you an example of how this function works.

Let's say I've got a few text files in the root of my C: drive that I want to copy to a destination share, as you see here.I want to copy these to a file share on a server of mine called LABDC with a file share called plcscript. These are critical text files, so I want to be absolutely sure the files that end up on my LABDC server are exactly the same files that are in the root of my C: drive -- with absolutely no exceptions.

I could just copy these files over and be done with it.

    PS C:\> Get ChildItem *.txt | Copy-Item -Destination \\labdc.lab.local\plcscript
    PS C:\>

I checked the share, the files are there, and I didn't get any errors. But how can I be absolutely sure these are the exact same files (down to the bit) that I had on my local computer with minimal effort? 

The answer is to simply replace Copy-Item with Copy-FileWithHashCheck. Let me show you how easy it is to add in this extra hash check while using this PowerShell function.In this example, I chose to use the–Verbose parameter so you can see as each file is copied to the destination, but you don't have to do this.  You'll also see it outputs True for each file. This means it successfully checked and matched both source and destination file hashes for each file.  If, for some reason, the hash check failed, you'd see False here.

    Download: Copy-FileWithHashCheck.ps1

Download the PowerShell function we used in this article and insert it into your PowerShell profile so it's available to you all the time.

RELATED:

More PowerShell How-Tos