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.

Become a Cyber Security Expert!:
Enroll in the Complete Cyber Security Course now, and master online safety.
Learn to defeat hackers, protect privacy, and stay anonymous with over 50 hours of on-demand video.

Table of Contents

Master Linux Commands
Your essential Linux handbook
Want to level up your Linux skills? Here is the perfect solution to become efficient on Linux. 20% off today!

Download now

How to use Get-FileHash in PowerShell?

Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!

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]

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:

Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!

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.

Master Ethical Hacking Skills!
Join the Complete Ethical Hacking Course Bundle and step into the world of cybersecurity.
Learn to think like a hacker and protect systems with this comprehensive course.

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)
$writer.write("MD5Online")
$writer.Flush()
$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.

Whenever you’re ready for more security, here are things you should think about:

- Break free from Gmail: You should be able to choose what happens to your data. With Proton, only you can read your emails. Get private email.

- Protect yourself online: Use a high-speed Swiss VPN that safeguards your privacy. Open-source, no activity logs. Get Proton VPN risk-free.

- Master Linux commands: A sure method to learn (and remember) Linux commands. Useful ones only, one at a time, with clear explanations. Download the e-book.

Similar Posts

Leave a Reply

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