Skip to main content

How to Get MD5 Hash in PowerShell

• 3 min read
powershell md5 hash cryptography security

In PowerShell, you can compute the MD5 hash value of a string or file using the Get-FileHash cmdlet. The Get-FileHash command uses the Algorithm parameter to specify the hash algorithm you want to use.

Get MD5 Hash of a String in PowerShell

To get the MD5 hash of a string in PowerShell, you can use the [System.Security.Cryptography.MD5] .NET class.

# Define the string
$someString = "This is some text for MD5 hash"

# Convert the string to bytes
$stringBytes = [System.Text.Encoding]::UTF8.GetBytes($someString)

# Create a MD5 hash object
$md5 = [System.Security.Cryptography.HashAlgorithm]::Create("MD5")

# Compute the MD5 hash
$hashBytes = $md5.ComputeHash($stringBytes)

# Convert the hash bytes to a hexadecimal string
$hashString = [System.BitConverter]::ToString($hashBytes)

$hashString = $hashString.Replace("-", "")

# Output the MD5 hash of a string
Write-Output $hashString

In this script, the $someString variable holds the string data. We convert the string into the bytes and store it into the $stringBytes variable.

[System.Security.Cryptography.MD5]::Create() method creates an instance of the MD5 hash algorithm. The ComputeHash() method computes the hash value of the input byte array. Later, we convert the hash bytes to a hexadecimal string.

Get MD5 Hash of a File in PowerShell

To get the MD5 hash of a file in PowerShell, you can use the Get-FileHash cmdlet with the MD5 Algorithm.

# Define the file path

$filePath = "C:\temp\log\system_log.txt"

# Get the file hash using the MD5 algorithm

$fileHash = Get-FileHash -Path $filePath -Algorithm MD5

# Output the hash of a file

Write-Output $fileHash.Hash

In the above PowerShell script, the $filePath variable stores the path to the file. The Get-FileHash cmdlet is used to compute the hash value of a file $filePath using the MD5 algorithm.

Finally, it outputs the MD5 file hash to the console.

Conclusion

I hope the above article on getting the MD5 hash value of a string or file in PowerShell is helpful to you.


File Information & Properties

File Management Operations

String Operations

Data Selection & Filtering

Display & Output

Control Flow & Logic

Variables & Collections

Output & Export

File Path Operations

  • PowerShell Test-Path - Check path existence
  • PowerShell Get-Item - Get item objects

Functions & Automation

Comprehensive Guides