Simple MD5 calculator in PowerShell

in #powershell7 years ago

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.

Sort:  

Congratulations @hoek! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

You published your First Post
You got a First Vote
Award for the number of upvotes

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

Upvote this notification to help all Steemit users. Learn why here!

Congratulations @hoek! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Do not miss the last post from @steemitboard:

3 years on Steem - The distribution of commemorative badges has begun!
Happy Birthday! The Steem blockchain is running for 3 years.
Vote for @Steemitboard as a witness to get one more award and increased upvotes!