How to get an MD5 checksum in PowerShell


I would like to calculate an MD5 checksum of some content. How do I do this in PowerShell?

This question is tagged with powershell powershell-2.0

~ Asked on 2012-05-09 17:24:26

The Best Answer is


If the content is a string:

$someString = "Hello, World!"
$md5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$utf8 = New-Object -TypeName System.Text.UTF8Encoding
$hash = [System.BitConverter]::ToString($md5.ComputeHash($utf8.GetBytes($someString)))

If the content is a file:

$someFilePath = "C:\foo.txt"
$md5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$hash = [System.BitConverter]::ToString($md5.ComputeHash([System.IO.File]::ReadAllBytes($someFilePath)))

Starting in PowerShell version 4, this is easy to do for files out of the box with the Get-FileHash cmdlet:

Get-FileHash <filepath> -Algorithm MD5

This is certainly preferable since it avoids the problems the first solution offers as identified in the comments (uses a stream, closes it, and supports large files).

~ Answered on 2012-05-09 17:32:52


If you are using the PowerShell Community Extensions there is a Get-Hash commandlet that will do this easily:

C:\PS> "hello world" | Get-Hash -Algorithm MD5

Algorithm: MD5

Path       :
HashString : E42B054623B3799CB71F0883900F2764

~ Answered on 2012-05-09 22:58:36

Most Viewed Questions: