Home » PowerShell » Get-FileHash in PowerShell- Get Hash of File

Get-FileHash in PowerShell- Get Hash of File

Very often, I need to perform MD5 checksum for file downloaded from internet or received from sender to check file transferred is correct and if not then consider file is corrupted. Using PowerShell Get-FileHash cmdlet, we can easily get md5 hash of file or SHA hash of file.

PowerShell Get-FileHash cmdlet calculate hash value of file using different hash algorithm. Hash values are used to compare two different files have same content or not. If both files has same hash value, it means files have same content. Get-File is useful when you want to check hash of file downloaded from internet.

To get hash of file, you can use MD5 or SHA algorithm to get hash of file and compare with other file. Get-FileHash Algorithm parameter allows to use different hashing algorithm as give below

  • MD5
  • SHA1
  • SHA256
  • SHA384
  • SHA512

Default Hash algorithm is SHA256.

MD5 Checksum -HASH Algorithm ISO Image
MD5 Checksum -HASH Algorithm ISO Image

In this blog post, I will explain you how to use PowerShell Get-FileHash cmdlet to get MD5 checksum or SHA checksum for file or iso image file in PowerShell.

PowerShell Get-FileHash

Cmdlet: Get-FileHash

Syntax:

Get-FileHash
   [-Path] <String[]>
   [[-Algorithm] <String>]
   [<CommonParameters>]

Get-FileHash cmdlet calculates hash value for a file using specified hashing algorithm. Hash assigns the unique value to the content of the file.

Let’s understand Get-FileHash cmdlet with different examples as given below

Using Get-FileHash to calculate Hash value for a File

Lets calculate hash value for ChecksumUtility.exe file using PowerShell Get-FileHash cmdlet

PS D:\Temp> Get-FileHash .\ChecksumUtility.exe

In the above example, using PowerShell Get-FileHash it calculate hash value for file. As default, Get-FileHash uses SHA256 algorithm, can be seen in below output image

Get-FileHash SHA-256 Algorithm - Hash Value
Get-FileHash SHA-256 Algorithm – Hash Value

PowerShell Tip: Get-ChildItem to search for files in directory using PowerShell!

Using Get-FileHash get md5 hash of file

For example purpose, I have downloaded Win2016_OS.iso file from MSDN account. We will check checksum of file downloaded is correct or not as per given for ISO file on MSDN website.

Use MD5 hash in PowerShell to calculate hash and get ISO file hash as given below

PS D:\Temp> Get-FileHash -Algorithm MD5 .\Win2016_OS.iso

In the above Get-FileHash example, using Algorithm parameter, MD5 hash in PowerShell get ms5 hash value for ISO image.

Output of the above command get md5 hash of file as below

MD5 Hash Algorithm - ISO image
MD5 Hash in PowerShell – ISO image

Cool Tip: Replace text in string using PowerShell!

Get SHA384 Hash of file

We will use above downloaded ISO file from internet to calculate SHA384 hash of file as below

PS D:\Temp> Get-FileHash -Algorithm SHA384 .\Win2016_OS.iso

In the above Get-FileHash example, it uses SHA384 hash algorithm specified by Algorithm parameter and get SHA384 hash of ISO file.

Output of SHA384 hash algorithm as below

SHA384 Hash Algorithm - ISO image
SHA384 Hash Algorithm – ISO image

Cool Tip: Best way to restart print spooler service in PowerShell!

Get-FileHash Recursive for files

If you want to get hash of all files in specified folder, use Get-FileHash command with Recurse parameter to recursively calculate hash of file using 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 calculate hash of files in specified folder and using Export-csv cmdlet in PowerShell, it get filehash output to csv file.

Conclusion

I hope you may find above article about how to use PowerShell Get-FileHash to get MD5 hash of file or SHA hash of file in PowerShell.

PowerShell Get-FileHash cmdlet with Algorithm parameter is used to compute hash value for file or ISO image file. Default value of Get-FileHash algorithm is SHA256.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on ShellGeek home page.

Leave a Comment