PowerShell – Get a file’s MD5 checksum

To get a file’s MD5 checksum, you can use Get-FileHash. This has been available since PowerShell v4. Use it like this:

$checksum = (Get-FileHash -Algorithm MD5 -Path C:\NFLTeamStats.json).Hash
Code language: PowerShell (powershell)

This file’s MD5 checksum is AE34D271ACC9C242BC9EED2E0EA72093.

If Get-FileHash isn’t available

Get-FileHash was put in PowerShell v4. If you’re on an earlier version, you’ll get following error when you try to use this:

Get-FileHash is not recognized as the name of a cmdlet, function, script file, or operable program

You have two options. You can either use the built-in .NET functionality or upgrade PowerShell.

Option 1 – Use MD5CryptoServiceProvider to get the MD5 checksum

If you can’t, or don’t want to, upgrade PowerShell to use Get-FileHash, then you can get the MD5 checksum with .NET, like this:

$md5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$checksumBytes = $md5.ComputeHash([System.IO.File]::ReadAllBytes("C:\NFLTeamStats.json"))
$checksum = [System.BitConverter]::ToString($checksumBytes).Replace("-", "")

$checksum
Code language: PowerShell (powershell)

As expected, this outputs AE34D271ACC9C242BC9EED2E0EA72093.

Option 2 – Install PowerShell v4 or above so you can use Get-FileHash

First check what version you’re on. Use the following to check what version of PowerShell you’re on:

$PSVersionTable.PSVersion
Code language: PowerShell (powershell)

In my case I’m on version 5.1, so the output looks like this.

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      18362  1171  Code language: plaintext (plaintext)

If you’re on a really old version of PowerShell, this might not return anything. That’s good enough proof that you’re on a version older than v4 and it’s time to upgrade.

Microsoft is always changing things, so it’s kind of risky to put links to their downloads. Instead, I’ll point you to their pages about installing the latest version. There are two variations – Windows PowerShell and just PowerShell (yup, they dropped Windows from the latest iteration).

After you get PowerShell upgraded, you’ll be able to use Get-FileHash, like this:

$checksum = (Get-FileHash -Algorithm MD5 -Path C:\NFLTeamStats.json).Hash
Code language: PowerShell (powershell)