hoek avatar

Simple MD5 calculator in PowerShell

hoek

Published: 27 Mar 2018 › Updated: 27 Mar 2018Simple MD5 calculator in PowerShell

Simple MD5 calculator in PowerShell

Compute the hash value

Get-FileHash cmdlet allows easily to compute the hash value for a file since PowerShell version 4.0. The acceptable values for algorithm are: SHA1, SHA256, SHA384, SHA512, MD5. More about Get-FileHash you can read in official documentation.
Simplest way to calculate md5 hash value for a file is to use command:

Get-FileHash path_to_file -Algorithm MD5 | Format-List

I decided to build simple MD5 calculator using Powershell.

MD5 calcuator

Powershell GUI

Menu

First I created a menu

function Show-Menu
{
     param (
           [string]$Title = 'MD5 Calculator'
     )
     cls
     Write-Host "===================== $Title ====================="

     Write-Host "1: Press '1' for single file, output in console"
     Write-Host "2: Press '2' for single file, output in text file"
     Write-Host "3: Press '3' for all files in folder, output in console"
     Write-Host "4: Press '4' for all files in folder, output in text file"
     Write-Host "Q: Press 'Q' to quit."
}

Variables

I used few variables which need to be provided by user:

  • $FilePath - for the file source
  • $FolderPath - for folder source
  • $Output - text file output location

Options

I added four options like:

  1. MD5 for single file, output in console Get-FileHash $FilePath -Algorithm MD5 | Format-List
  2. MD5 for single file, output in text file Get-FileHash $FilePath -Algorithm MD5 | Format-List > $Output\MD5.txt
  3. MD5 for all files in folder, output in console Get-ChildItem $FolderPath | Get-FileHash -Algorithm MD5 | Format-List
  4. MD5 for all files in folder, output in text file Get-ChildItem $FolderPath | Get-FileHash -Algorithm MD5 | Format-List > $Output\MD5.txt

Progress info

I also wanted to display some information about work in progress so I added three text variables

  • $Activity = "Calculating MD5 Hash Value"
  • $Id = 1
  • $Task = "Please wait"

and part of code to display progress info during the hash value is calculated

Write-Progress -Id $Id -Activity $Activity -Status $Task

Whole script

And the full code looks like below

function Show-Menu
{
     param (
           [string]$Title = 'MD5 Calculator'
     )
     cls
     Write-Host "===================== $Title ====================="

     Write-Host "1: Press '1' for single file, output in console"
     Write-Host "2: Press '2' for single file, output in text file"
     Write-Host "3: Press '3' for all files in folder, output in console"
     Write-Host "4: Press '4' for all files in folder, output in text file"
     Write-Host "Q: Press 'Q' to quit."
}
do
{
     Show-Menu
     $input = Read-Host "Please make a selection"
     switch ($input)
     {
           '1' {
                cls
                'MD5 for single file, output in console'
                'Paste UNC path to file you want to calculate'
                $FilePath = Read-Host -Prompt 'Source file path'
                $Activity = "Calculating MD5 Hash Value"
                $Id       = 1
                $Task     = "Please wait"
                Write-Progress -Id $Id -Activity $Activity -Status $Task
                Get-FileHash $FilePath -Algorithm MD5 | Format-List
           } '2' {
                cls
                'MD5 for single file, output in text file'
                'Paste UNC path to file you want to calculate'
                $FilePath = Read-Host -Prompt 'Source file path'
                'Paste path for output report eg: C:\reports'
                $Output = Read-Host -Prompt 'Report location path'
                $Activity = "Calculating MD5 Hash Value"
                $Id       = 1
                $Task     = "Please wait"
                Write-Progress -Id $Id -Activity $Activity -Status $Task
                Get-FileHash $FilePath -Algorithm MD5 | Format-List > $Output\MD5.txt
                'Complete! Report saved to MD5.txt file'
           } '3' {
                cls
                'MD5 for all files in folder, output in console'
                'Paste UNC path to folder with files you want to calculate'
                $FolderPath = Read-Host -Prompt 'Source folder path'
                $Activity = "Calculating MD5 Hash Value"
                $Id       = 1
                $Task     = "Please wait"
                Write-Progress -Id $Id -Activity $Activity -Status $Task
                Get-ChildItem $FolderPath | Get-FileHash -Algorithm MD5 | Format-List

           } '4' {
                cls
                'MD5 for all files in folder, output in text file'
                'Paste UNC path to folder with files you want to calculate'
                $FilePath = Read-Host -Prompt 'Source folder path'
                'Paste path for output report eg: C:\reports'
                $Output = Read-Host -Prompt 'Report location path'
                $Activity = "Calculating MD5 Hash Value"
                $Id       = 1
                $Task     = "Please wait"
                Write-Progress -Id $Id -Activity $Activity -Status $Task
                Get-ChildItem $FolderPath | Get-FileHash -Algorithm MD5 |Format-List > $Output\MD5.txt
                'Complete! Report saved to MD5.txt file'
           } 'q' {
                return
           }
     }
     pause
}
until ($input -eq 'q')

Usage

All you need is to copy everything and save as MD5Calculator.ps1 then run with Powershell and follow instructions on the screen. By changing the -Algorithm parameter you can simply create a calculator for other algorithms.

Leave Simple MD5 calculator in PowerShell to:

Written by

:)

Read more #powershell posts


Best Posts From hoek

We have not curated any of hoek's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From hoek