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:

1
Get-FileHash path_to_file -Algorithm MD5 | Format-List

I decided to build simple MD5 calculator using Powershell.

MD5 calcuator

Powershell GUI

First I created a menu

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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

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

Whole script

And the full code looks like below

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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.