Very often, I need to perform MD5 checksum for files downloaded from the internet or received from the sender to check file transferred is correct, and if not then consider the file corrupted. Using the PowerShell Get-FileHash cmdlet, we can easily get the md5 hash of the file or the SHA hash of the file.
PowerShell Get-FileHash cmdlet calculates the hash value of the file using different hash algorithms. Hash values are used to compare whether two different files have the same content or not. If both files have the same hash value, it means the files have the same content. Get-File is useful when you want to check the hash of a file downloaded from the internet.
You can use the MD5 or SHA algorithm to get the hash of a file and compare it with another file. The PowerShell Get-FileHash uses the Algorithm parameter that allows to use of different hashing algorithms as given below.
The default Hash algorithm is SHA256.
In this blog post, we will discuss how to use the PowerShell Get-FileHash cmdlet to get the MD5 checksum or SHA checksum for a file or iso image file in PowerShell.
Get-FileHash [-Path] <String> [[-Algorithm] <String>] [<CommonParameters>]
Get-FileHash cmdlet calculates the hash value for a file using a specified hashing algorithm. Hash assigns a unique value to the content of the file.
Let’s understand the Get-FileHash cmdlet with different examples as given below.
Using Get-FileHash to calculate Hash value for a File
Let’s calculate the hash value for the ChecksumUtility.exe file using the PowerShell Get-FileHash cmdlet.
PS D:\Temp> Get-FileHash .\ChecksumUtility.exe
In the above example, using PowerShell Get-FileHash calculate the hash value for the file. As default, Get-FileHash uses the SHA256 algorithm, which can be seen in the output image below.
PowerShell Tip: Get-ChildItem to search for files in the directory using PowerShell!
Using Get-FileHash get the md5 hash of the file
For example purposes, I have downloaded the Win2016_OS.iso file from the MSDN account. We will check whether the checksum of a file downloaded is correct or not as given for the ISO file on the MSDN website.
Use MD5 hash in PowerShell to calculate a hash and get the ISO file hash as given below
PS D:\Temp> Get-FileHash -Algorithm MD5 .\Win2016_OS.iso
In the above Get-FileHash example, using the
Algorithm parameter, MD5 hash in PowerShell gets the md5 hash value for the ISO image.
The output of the above command gets the md5 hash of a file as below.
Cool Tip: Replace text in a string using PowerShell!
Get SHA384 Hash of file in PowerShell
We will use the above downloaded ISO file from the internet to calculate the SHA384 hash of a file as below.
PS D:\Temp> Get-FileHash -Algorithm SHA384 .\Win2016_OS.iso
The above Get-FileHash example uses the
SHA384 hash algorithm specified by the
Algorithm parameter and gets the SHA384 hash of the ISO file.
Output of SHA384 hash algorithm as below.
Cool Tip: The best way to restart print spooler service in PowerShell!
Get File Hash for files in PowerShell
If you want to get the hash of all files in a specified folder, use the Get-FileHash command with the
Recurse parameter to recursively calculate the hash of the file using the default SHA256 algorithm and export get file hash to csv file.
Get-ChildItem -Path D:\PowerShell\ -Recurse -Filter *.exe | Get-FileHash | Export-Csv -Path D:\PowerShell\FilesHash.csv -NoTypeInformation
In the above example, Get-FileHash recursive calculates the hash of files in a specified folder, and using the Export-CSV cmdlet in PowerShell, it gets file hash output to a csv file.
I hope you find the above article about how to use PowerShell Get-FileHash to get the MD5 hash of a file or SHA hash of the file in PowerShell.
PowerShell Get-FileHash cmdlet with
Algorithm parameter is used to compute the hash value for a file or ISO image file. The default value of the Get-FileHash algorithm is SHA256.
You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.