Powershell

How to Get an MD5 Checksum in PowerShell

MD5 is the message-digest algorithm used to encrypt files, passwords, credit card details, and other sensitive information. It is also used to verify or match whether the file you sent to a person and the file that the receiver has received is the same. Moreover, Windows PowerShell can utilize it to get the fingerprint of a specific file. MD5 is an old and weak hashing algorithm, but it is still used in various departments. Moreover, PowerShell includes a cmdlet to create and get an MD5 checksum for a specific file.

This post will demonstrate a procedure to create and get the checksum of a file.

How to Get an MD5 Checksum in PowerShell?

You can use these methods to get the MD5 checksum for a file in PowerShell:

Method 1: Get an MD5 Checksum in PowerShell Using MD5 Hashing Algorithm

Despite the fact that the MD5 is an obsolete tool to encrypt files, it is still one of the best utilities to check whether the file has been tampered with or not.

Basic Syntax
This is the basic syntax of the MD5 hashing algorithm:

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

Here:

  • Get-FileHash” command is used to calculate the hash value of the file specified by a certain hash algorithm.
  • [-Path] <file>” refers to the path of the selected file.
  • -Algorithm” cmdlet is used here to define the “<algo>” algorithm.
  • Options” refers to the supported options.

Example
In this example, we will generate the “MD5” checksum for the “Notepad.exe” file.

> Get-FileHash C:\Windows\notepad.exe -Algorithm MD5

The output shows that the MD5 checksum has been generated successfully.

Method 2: Get an MD5 Checksum in PowerShell Using Hashing String

Another method to get the MD5 checksum is the “Hashing String” method. However, there is no direct method to hash the string in PowerShell, but still, you can use the “Get-FileHash” cmdlet with the combination of the “Stream” parameter.

Example
In the given code example, we have created a script containing a string stream to generate md5 checksum:

$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

Here:

  • $stringAsStream” is utilized to create a new string stream using the “new()” constructor of the “[System.IO.MemoryStream]” class.
  • $writer” is used to write on the stream by passing the created string as a parameter to the “new()” constructor of the “[System.IO.StreamWriter]” class.
  • MD5Online” will be added to the “$writer”.
  • Get-FileHash” is used finally to generate the checksum by specifying “MD5” as the required algorithm.

Output

As you can see, the output confirms that the MD5 checksum has been generated successfully.

Conclusion

To get an MD5 checksum in PowerShell, use the “Get-FileHash” cmdlet followed by the file <path>, “-Algorithm” flag, and the “MD5” parameter. Then, execute it to get the MD5 checksum. This post has discussed different approaches to get the MD5 checksum in PowerShell.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.