Simple MD5 calculator in PowerShell

in #powershell7 years ago

Compute the hash value

<p dir="auto"><code>Get-FileHash cmdlet allows easily to compute the <a href="https://en.wikipedia.org/wiki/Hash_function" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">hash value for a file since PowerShell version 4.0. The acceptable values for algorithm are: SHA1, SHA256, SHA384, SHA512, MD5. More about <code>Get-FileHash you can read in <a href="https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/get-filehash?view=powershell-6" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">official documentation.<br /> Simplest way to calculate md5 hash value for a file is to use command: <pre><code>Get-FileHash path_to_file -Algorithm MD5 | Format-List <p dir="auto">I decided to build simple MD5 calculator using <a href="https://en.wikipedia.org/wiki/PowerShell" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Powershell. <h2>MD5 calcuator <p dir="auto">Powershell GUI <h3>Menu <p dir="auto">First I created a menu <pre><code>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." } <h3>Variables <p dir="auto">I used few variables which need to be provided by user: <ul> <li><code>$FilePath - for the file source <li><code>$FolderPath - for folder source <li><code>$Output - text file output location <h3>Options <p dir="auto">I added four options like: <ol> <li>MD5 for single file, output in console <code>Get-FileHash $FilePath -Algorithm MD5 | Format-List <li>MD5 for single file, output in text file <code>Get-FileHash $FilePath -Algorithm MD5 | Format-List > $Output\MD5.txt <li>MD5 for all files in folder, output in console <code>Get-ChildItem $FolderPath | Get-FileHash -Algorithm MD5 | Format-List <li>MD5 for all files in folder, output in text file <code>Get-ChildItem $FolderPath | Get-FileHash -Algorithm MD5 | Format-List > $Output\MD5.txt <h3>Progress info <p dir="auto">I also wanted to display some information about work in progress so I added three text variables <ul> <li><code>$Activity = "Calculating MD5 Hash Value" <li><code>$Id = 1 <li><code>$Task = "Please wait" <p dir="auto">and part of code to display progress info during the hash value is calculated <pre><code>Write-Progress -Id $Id -Activity $Activity -Status $Task <h2>Whole script <p dir="auto">And the full code looks like below <pre><code>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') <h3>Usage <p dir="auto">All you need is to copy everything and save as <strong>MD5Calculator.ps1 then run with Powershell and follow instructions on the screen. By changing the <code>-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) :

<p dir="auto"><a href="http://steemitboard.com/@hoek" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link"><img src="https://images.hive.blog/768x0/https://steemitimages.com/70x80/http://steemitboard.com/notifications/firstpost.png" srcset="https://images.hive.blog/768x0/https://steemitimages.com/70x80/http://steemitboard.com/notifications/firstpost.png 1x, https://images.hive.blog/1536x0/https://steemitimages.com/70x80/http://steemitboard.com/notifications/firstpost.png 2x" /> You published your First Post<br /> <a href="http://steemitboard.com/@hoek" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link"><img src="https://images.hive.blog/768x0/https://steemitimages.com/70x80/http://steemitboard.com/notifications/firstvoted.png" srcset="https://images.hive.blog/768x0/https://steemitimages.com/70x80/http://steemitboard.com/notifications/firstvoted.png 1x, https://images.hive.blog/1536x0/https://steemitimages.com/70x80/http://steemitboard.com/notifications/firstvoted.png 2x" /> You got a First Vote<br /> <a href="http://steemitboard.com/@hoek" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link"><img src="https://images.hive.blog/768x0/https://steemitimages.com/70x80/http://steemitboard.com/notifications/votes.png" srcset="https://images.hive.blog/768x0/https://steemitimages.com/70x80/http://steemitboard.com/notifications/votes.png 1x, https://images.hive.blog/1536x0/https://steemitimages.com/70x80/http://steemitboard.com/notifications/votes.png 2x" /> Award for the number of upvotes <p dir="auto">Click on any badge to view your own Board of Honor on SteemitBoard.<br /> For more information about SteemitBoard, click <a href="https://steemit.com/@steemitboard" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">here <p dir="auto">If you no longer want to receive notifications, reply to this comment with the word <code>STOP <blockquote> <p dir="auto">Upvote this notification to help all Steemit users. Learn why <a href="https://steemit.com/steemitboard/@steemitboard/http-i-cubeupload-com-7ciqeo-png" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">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!