Get an MD5 Hash in PowerShell

How to Get an MD5 Hash in PowerShell?

MD5 is a hashing algorithm, still popular despite the security issues. You can use it to encrypt a string or to get the fingerprint of a file. In this article, we’ll see how to use it in PowerShell.

PowerShell offers a cmdlet to generate MD5 hash for a file: Get-FileHash. It can also be used to get the MD5 hash for a string, by opening a stream and hashing it.

I’ll start by showing you how to use this cmdlet for a file, which is its main purpose. And then we’ll see how you can use it to hash strings too.

Hide your IP address and location with a free VPN:
Try it for free now, companies are paying for you.
1500 servers in 120 countries. It's free. Forever. No email required.

Table of Contents

How to use Get-FileHash in PowerShell?

Stay tuned with the latest security news!
You'll enjoy receiving the recent articles directly in your inbox every week!

Even if MD5 is no longer safe to use for encryption, it’s still an excellent solution to quickly check if a file transfer has been successful or not. I explain everything in this article, but in short the idea is to get the MD5 fingerprint of the file before and after the transfer. If it’s the same value, the file transfer is OK, if not the file is corrupted.

The Get-FileHash syntax

The Get-FileHash cmdlet display the hash value of a file. By default, it uses the SHA256 algorithm, but we can add an extra parameter to use MD5.

Here is the cmdlet syntax:
Get-FileHash [-Path] <file> [[-Algorithm] <algo>] [Options]
So, the file path is mandatory, and then we can specify the hashing algorithm and a few other options we don’t need in our case.

It can also be used with a stream instead of a file path. I give you the syntax directly, and we’ll see later how to use this to hash a string in PowerShell.
Here is the syntax for a stream input:
Get-FileHash [-InputStream] <stream> [[-Algorithm] <algo>] [Options]

Complete Security Course
Become a cyber security specialist.
Network Security, WiFi Security, WiFi Hackers, Firewalls, Wireshark, Secure Networking. + Password Managers.

Learn more

Get-FileHash example

Ok, we know the syntax, let’s see how to use it.
It’s not complicated, here is an example:
Get-FileHash C:\Windows\explorer.exe -Algorithm MD5

You’ll get something like this as a result:

Stay tuned with the latest security news!
You'll enjoy receiving the recent articles directly in your inbox every week!

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

If you are using PowerShell in a script, you can create a variable with the result ($hash for example) and get the hash value with $hash.Hash to make sure it’s the same value as the original file.

How to hash a string in PowerShell?

Unfortunately, there is no direct function 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 script code, and I’ll explain after that:

$stringAsStream = [System.IO.MemoryStream]::new()
$writer = [System.IO.StreamWriter]::new($stringAsStream)
$stringAsStream.Position = 0
Get-FileHash -InputStream $stringAsStream -Algorithm MD5
  • So, we start by creating a new stream ($stringAsStream)
  • Then we write on the stream ($writer)
  • And finally we hash the stream content with Get-FileHash.
    The only change compared to the first part of this tutorial, is that we use -InputStream instead of -Path
  • As you can see on the picture, we get the MD5 hash of our string as a result.

It’s not the easiest language to generate MD5 hashes from a string (PHP and Python have native functions for example), but it works 🙂
If you need to use this often in your script, I would recommend creating a function that you can use each time it’s needed.

Similar Posts

Leave a Reply

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