Using Powershell to create MD5 files

This entry is part 2 of 4 in the series MD 5 Calculation

Following up on an article a few days ago here’s a script that creates a MD5 file.

# Uwe Ziegenhagen, 12.11.2014
# Reference:
### Step A: Get the name of the file
Function Get-FileName($initialDirectory)
 [System.Reflection.Assembly]::LoadWithPartialName("") | Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
$OpenFileDialog.filter = "All files (*.*)| *.*"
$OpenFileDialog.ShowDialog() | Out-Null
} #end function Get-FileName
# *** Entry Point to Script ***
$file =  Get-FileName -initialDirectory "C:\"
# write-host "file: " $file
### Step B: Create a file handle from this string and calculate the MD5 sum
$filehandle = Get-ChildItem $file
write-host "Filename: " $filehandle.Name
$md5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$hash = [System.BitConverter]::ToString($md5.ComputeHash([System.IO.File]::ReadAllBytes($filehandle))).ToLower().Replace("-","")
write-host "Calculated Hashsum: "  $hash
### Step C: Write the MD5 sum from the MD5 sum file matching the other filename
$hash | out-file -FilePath $($filehandle.DirectoryName + "\" + $filehandle.BaseName + ".md5")
Series Navigation<< Using Powershell to calculate/compare MD5 hashesUsing Python to compute a MD5 Hash >>


Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website