Get the MD5 Checksum in PowerShell

  1. Using the MD5 Hashing Algorithm in PowerShell
  2. Hashing Strings in PowerShell
Get the MD5 Checksum in PowerShell

MD5 is a hashing algorithm that is still widely used despite having weak security issues. We can use it to encrypt a string or get the fingerprint of a file.

Windows PowerShell offers a cmdlet to generate MD5 hash for a file. The cmdlet can also get the MD5 hash for a string by opening a stream and hashing it.

This article will understand the hashing algorithm and use it in PowerShell.

Using the MD5 Hashing Algorithm in PowerShell

Even if MD5 is not recommended for security, it’s still an excellent solution to check if a file transfer has been tampered with or successful.

First, get the MD5 fingerprint of the file before and after the transfer.

If it results in the same value, the file transfer has not been tampered with. If not, it’s corrupted.

To do this, we will use the Get-FileHash cmdlet. The Get-FileHash cmdlet displays the hash value of a file.

It uses the SHA256 algorithm by default, but we can add an extra parameter to use MD5.

Get-FileHash [-Path] <file> [[-Algorithm] <algo>] [Options]

We can also use it with a stream instead of a file path.

Get-FileHash [-InputStream] <stream> [[-Algorithm] <algo>] [Options]

Here is an example:

Get-FileHash C:\Windows\explorer.exe -Algorithm MD5


Algorithm       Hash                                                                   Path
---------       ----                                                                   ----
MD5             3F786F7D200D0530757B91C5C80BC049                                       C:\Windows\explorer.exe

The -Path argument is not mandatory, so we don’t need to use it. Instead, we give the file path and add the algorithm parameter to use MD5 instead of SHA256.

Using PowerShell in a script, we can create a variable with the result and get the hash value with the hash property like $variable.Hash to ensure it’s the same value as the original file.

Hashing Strings in PowerShell

Unfortunately, there is no direct function or native commands to generate a hash from a string in PowerShell.

However, it’s possible to use Get-FileHash with a stream parameter, so it’s a solution to compute the hash of a string.

Let’s start directly with the snippet below:

$stringAsStream = [System.IO.MemoryStream]::new()
$writer = [System.IO.StreamWriter]::new($stringAsStream)
$stringAsStream.Position = 0
Get-FileHash -InputStream $stringAsStream -Algorithm MD5


Algorithm       Hash                                                                   
---------       ----                                                                   
MD5             D49019C7A78CDAAC54250AC56D0EDA8A
  • Start by creating a new stream using $stringAsStream.
  • Then write on the stream using $writer.
  • Finally, we hash the stream content with Get-FileHash.

Compared to the first section of this article, the only change is that we used -InputStream instead of -Path.

As you can see on the output, we get the MD5 hash of our string as a result.

Marion Paul Kenneth Mendoza avatar Marion Paul Kenneth Mendoza avatar

Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.