Do it the easy way: Working with file hashes in PowerShell

A while back I wrote an article on this site in which I discussed a PowerShell error indicating that a term is not recognized as a cmdlet. A few days ago, someone commented on the article that they were receiving this particular error while trying to calculate an MD5 hash. I found the source of their problem and explained why they were receiving the error. However, I had never attempted to create a file hash in PowerShell and wasn’t quite sure what was involved. Not having time to look it up at that moment, I suggested that they search the Internet for a script. Ever since I posted that advice, it has bothered me that I didn’t come up with something more helpful. As such, I wanted to take the opportunity to demonstrate how file hashing works in PowerShell.

A simple process

Using PowerShell to create a file hash is a surprisingly simple process. There is actually a native PowerShell cmdlet that can be used to create a file hash. That cmdlet is named Get-FileHash. To use the cmdlet, simply append the name of the path for which you wish to create a hash. For example, I have a file on my hard disk named Nano.vhd. If I wanted to hash that file, I would use this command:

Get-FileHash C:\Nano.vhd

If you look at the command’s output in the figure below, you can see that PowerShell created a SHA256 hash of the file. However, this is not the only type of hash that is supported. By appending the Algorithm parameter, it is possible to create a different type of hash (such as MD5).

file hashes
To specify a different hashing algorithm, just add the -Algorithm parameter to the end of the command shown above, followed by the name of the algorithm that you wish to use. The supported algorithms include SHA1, SHA256, SHA384, SHA512, and MD5. If you wanted to create an MD5 hash, for example, you would use a command similar to this one:

Get-FileHash C:\Nano.vhd -Algorithm MD5

You can see the command’s output in the figure below.

file hashes

As well as this technique works, the basic techniques that I have shown you are kind of impractical. After all, there is rarely a real-world use case for reading a file hash off of the screen. More often, hashes are used as a basis for comparing files against one another. It is possible to make such a comparison using PowerShell, but before we can do that we have to write the hash value to a variable.

When you hash a file in PowerShell, the output contains three individual pieces of information – the algorithm, the hash, and the path and file name. In order to perform any sort of comparison, we have to isolate the hash value from the rest of the command’s output. The easiest way to write the file hash to a variable is to use a command like this one:

$File1S = (Get-FileHash C:\Nano.vhd -Algorithm MD5).Hash

This command creates a variable named $File1. The command works by executing the Get-FileHash cmdlet on the file of interest (while also specifying the desired hashing algorithm) and then writing the output’s Hash attribute to the $File1 variable. You can see the command, as well as a subsequent command that displays the variable’s contents, shown in the figure below.

file hashes

Comparing two files

So now let’s use PowerShell to compare the hashes of two different files. I have two separate, but similar virtual hard disk files stored on my hard drive. One of these files, Nano.vhd, is a VHD-based virtual hard disk file. The other file, Nano.vhdx, is another virtual hard disk file. The contents of the two virtual hard disks are identical to one another, but the files themselves are different because they are in two different formats (VHD vs VHDX). Therefore, the two virtual hard disks should have dissimilar hashes.

The first thing that I am going to do is to use the technique described above to hash both files. This time, I will create two variables, one for each file. I will call these variables $File1 and $File2. If I output the contents of the two variables, I can see right away that the hashes are different, as shown in the figure below. But just for the sake of demonstration, let’s let PowerShell make the comparison.

file hashes

PowerShell makes it easy to compare the contents of two variables. You must begin by using the If command. From there, you would write out your comparison inside of parenthesis. This essentially involves typing one variable name followed by a comparison operator and the other variable name. In this case, the two most relevant comparison operators are -EQ (is equal to) and -NE (not equal to). The last step in the process is to take some sort of action based on the comparison. For example, you might display a line of text to convey the results of the comparison. Whatever action you take, the command for doing so must be enclosed in braces.

So here are a couple of examples. The first command listed below checks to see if the hashes are equal and if they are found to be equal displays the words “the files are identical to one another.” The second command checks to see if the hashes are not equal to one another. If the hashes are found to be dissimilar then the command displays the text “the files are different from one another”. Here are the commands:

if ($File1 -eq $File2) {Write "The files are identical to one another"}
if ($File1 -ne $File2) {Write "The files are different from one another"}

You can see the output of these commands in the figure below.

file hashes

 

File hashes the easy way

PowerShell makes it surprisingly easy to create file hashes. In fact, there is a built-in command for doing just that. From there, you can easily make hash comparisons using native PowerShell cmdlets.

About The Author

1 thought on “Do it the easy way: Working with file hashes in PowerShell”

  1. Hi Brien, thanks for the informative article, may i ask why in the last example you do two separate comparison instead of just one IF {} ELSE {} ? is there something behind it?

Leave a Comment

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top