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:
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.
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:
$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.