How to get an MD5 checksum in PowerShell

In this article, We will learn How to Generate an MD5 checksum in PowerShell Script. Before that let’s take a look at what exactly is MD5 checksum and why do you need to use MD5?

What is MD5 Checksum?

MD5 checksum is a computer program that calculates and verifies 128-bit MD5 hashes, as described in RFC 1321. The MD5 hash (or checksum) functions as a compact digital fingerprint of a file.

Why do we need to use MD5 Checksum and What is the benefit of it?

As we know MD5 checksum is a digital fingerprint of a file. We download many files from the internet without knowing the integrity of the file. Hence with most of the files, there will be an MD5 checksum  also called as an MD5 hash key will be associated with it. Once you download the file and the hash matches up then there is nothing to worry about it. If it fails then the file has tampered or modified. This might be a serious security threat or an integrity failure.

We do have various algorithms in different languages to calculate the MD5 hashing for files and string. PowerShell version 4 onwards we can achieve this in a straightforward way. The older version is little tricky. Let’s take a look at it in the below examples.

Generate an MD5 checksum in PowerShell Version 1, 2 and 3

We need to use the MD5CryptoServiceProvider to generate the MD5 hash in the PowerShell versions 1, 2, and 3.

Generate MD5 hash for 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)))

Generate MD5 checksum for file

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

Get an MD5 checksum in PowerShell version 4 and above

Starting in PowerShell version 4 and above, there is a very easy way to generate the MD5 has. PowerShell comes up with a cmdlet called Get-FileHash

Get-FileHash <filepath> -Algorithm MD5

Get-FileHash C:\foo.txt -Algorithm MD5


Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like
XOR in Python

XOR in Python

XOR Operator in Python is also known as “exclusive or”  that compares two binary numbers bitwise if two bits are…
How To

How to configure Git proxy?

If you are working in the corporate company all the internet requests usually goes through the corporate firewall.…