How to Get MD5 Hash of a File in PowerShell?

MD5, which stands for Message Digest Algorithm 5, produces a unique 128-bit hash value for a given data set. While MD5 is no longer recommended for cryptographic purposes due to vulnerabilities, it’s still widely used for file integrity checks. PowerShell makes it easy to compute the MD5 hash of files. In this PowerShell post, I will show you various methods of obtaining the MD5 hash of a file using PowerShell.

To generate an MD5 hash of a file in PowerShell, you can use the Get-FileHash cmdlet. Simply execute Get-FileHash -Algorithm MD5 -Path “C:\MyFolder\file.txt” to obtain the hash. For earlier versions of PowerShell or for more detailed scripting, you can utilize the .NET Framework’s System.Security.Cryptography.MD5CryptoServiceProvider class to compute the hash programmatically.

Get MD5 Hash of a File Using Get-FileHash Cmdlet

To get the MD5 hash of a file in PowerShell, use the Get-FileHash cmdlet. This cmdlet is available in PowerShell version 4.0 and later, and it supports different hash algorithms, including MD5.

Here is an example.

Get-FileHash -Algorithm MD5 -Path "C:\MyFolder\Users.txt"

Here is the complete PowerShell script.

# Define the file path
$filePath = "C:\MyFolder\Users.txt"

# Compute the hash value
$hash = Get-FileHash -Algorithm MD5 -Path $filePath

# Output the hash value
$hash.Hash

By saving this script as a .ps1 file, you can easily run it to get the MD5 hash of any file by replacing the $filePath variable with the path to your target file.

You can see the output in the screenshot below after executing the PowerShell script using vs code.

Get MD5 Hash of a File in PowerShell

Get MD5 Hash of a File in PowerShell Using .NET Framework Classes

If you’re working with an older version of PowerShell or prefer a more programmatic approach, you can use the .NET Framework to compute the MD5 hash.

Here is a complete example of getting the hash of a file in PowerShell.NET framework classes.

# Load the necessary .NET Framework class
[System.Reflection.Assembly]::LoadWithPartialName("System.Security.Cryptography")

# Create an MD5 Crypto Service Provider object
$md5 = New-Object System.Security.Cryptography.MD5CryptoServiceProvider

# Compute the hash value for the file
$hash = [System.BitConverter]::ToString($md5.ComputeHash([System.IO.File]::ReadAllBytes("C:\MyFolder\file.txt")))

# Output the hash value
$hash -replace '-', ''

This script uses the MD5CryptoServiceProvider class to compute the hash and the BitConverter class to format it into a readable string.

Write a Reusable Function to Get MD5 Hash of a File

You can write a complete function that you can reuse to get the MD5 Hash of a file using PowerShell.

Here is a complete example.

function Get-MD5Hash {
    param (
        [string]$filePath
    )
    try {
        $hash = Get-FileHash -Algorithm MD5 -Path $filePath
        return $hash.Hash
    }
    catch {
        Write-Error "An error occurred: $_"
    }
}

# Usage
Get-MD5Hash -filePath "C:\MyFolder\file.txt"

This function encapsulates the Get-FileHash cmdlet and can be called with different file paths to obtain the MD5 hash.

MD5 Hash Checks For Multiple Files using PowerShell

If you want to check the MD5 hash checks for multiple files using PowerShell, you can automate the process by using a custom function that can iterate through files in a directory.

Here is an example with the complete script.

# Directory path
$directoryPath = "C:\MyFolder"

# Get all files in the directory
$files = Get-ChildItem -Path $directoryPath

# Iterate through each file and compute the hash
foreach ($file in $files) {
    $hash = Get-FileHash -Algorithm MD5 -Path $file.FullName
    Write-Host "File: $($file.Name) - MD5 Hash: $($hash.Hash)"
}

This script will output the MD5 hash for each file in the specified directory.

Conclusion

I hope this practical approach tutorial will help you learn how to get the MD5 Hash of a File in PowerShell using various methods. Like:

  • Using Get-FileHash Cmdlet
  • Using .NET Framework Classes

I have also explained how to create a function that you can reuse to get the MD5 Hash of multiple files in PowerShell.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.