Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # generatemd5.ps1 by HTWingNut 06 June 2023
- Clear-Host
- Write-Host ""
- $datetime = Get-Date
- $timestamp = $datetime.ToString("yyyyMMdd_HHmmss")
- $reqpath = 'Input path to generate md5 hashes'
- $basepath = $hash1 = $hash2 = "?"
- $choice=$null
- $md5log = 'hashes_'+$timestamp+'.md5'
- function GenerateHash {
- Clear-Host
- Write-Host ""
- Write-Host "Generate Hash Files ..."
- Write-Host ""
- while (!(Test-Path -Path $basepath)) { $basepath = Read-Host -Prompt $reqpath; if ( $basepath -eq "") { $basepath="?" } }
- $numfiles = ( Get-ChildItem -Path "$basepath" -Recurse -File | Measure-Object ).Count
- $lchar = $basepath.Substring($basepath.length-1,1)
- If ($lchar -eq "\") { $basepath = $basepath.Substring(0,$basepath.length-1) }
- Write-Host "Total Files: $numfiles"
- Write-Host ""
- $count=1
- Write-Output "**** $($datetime) '$($basepath)'" | Set-Content "$md5log"
- Get-ChildItem -Path "$basepath" -Recurse -File |
- Get-FileHash -Algorithm MD5 |
- Select-Object Hash,Path |
- ForEach-Object {
- $_.Path = ($_.Path -replace [regex]::Escape("$basepath"), '')
- Write-Host "$($count) of $($numfiles)" $_.Hash $_.Path
- ++$count
- $_
- } |
- Format-Table -AutoSize -HideTableHeaders |
- Out-String -Width 4096 |
- Out-File "$md5log" -encoding ASCII -append
- (get-content $md5log).Trim() -ne '' | Set-Content $md5log
- Write-Host ""
- Write-Host "Hashes stored in file '$pwd\$md5log'"
- Write-Host ""
- }
- function CompareHash {
- $continue = "n"
- While ([bool]$continue) {
- Clear-Host
- Write-Host ""
- Write-Host "Compare Hash Files..."
- $hash1 = $hash2 = "?"
- $numksip1 = $numskip2 = "0"
- Write-Host ""
- while (!(Test-Path $hash1)) { $hash1 = Read-Host -Prompt "Enter Path/Name for Log 1"; if ( $hash1 -eq "") { $hash1="?" } }
- Write-Host ""
- Write-Host "First 9 lines of $($hash1):"
- $i=1; Get-Content $hash1 | Select -First 9 | % {"$($i) $_"; $i++}
- Write-Host ""
- Do { $numskip1 = Read-Host "Choose Number of Lines to Skip [0-9] (default 0)" } until ($numskip1 -in 0,1,2,3,4,5,6,7,8,9)
- Write-Host ""
- while (!(Test-Path $hash2)) { $hash2 = Read-Host -Prompt "Enter Path/Name for Log 2"; if ( $hash2 -eq "") { $hash2="?" } }
- Write-Host ""
- Write-Host "First 9 lines of $($hash2):"
- $i=1; Get-Content $hash2 | Select -First 9 | % {"$($i) $_"; $i++}
- Write-Host ""
- Do { $numskip2 = Read-Host "Choose Number of Lines to Skip [0-9] (default 0)" } until ($numskip2 -in 0,1,2,3,4,5,6,7,8,9)
- $hclog = "HashCompare_$(((Get-Item $hash1).Basename).Replace(' ','_'))_vs_$(((Get-Item $hash2).Basename).Replace(' ','_'))_$timestamp.txt"
- Write-Host ""
- Write-Host "---------------------"
- Write-Host ""
- Write-Host "**** File: '$((Get-Item $hash1).Name)'"
- Write-Host "**** Skipping Lines:"
- Get-Content $hash1 | Select -First $numskip1
- Write-Host ""
- Write-Host "**** Starting with Line:"
- Get-Content $hash1 | Select -Index ($numskip1)
- Write-Host ""
- Write-Host "---------------------"
- Write-Host ""
- Write-Host "**** File: '$((Get-Item $hash2).Name)'"
- Write-Host "**** Skipping Lines:"
- Get-Content $hash2 | Select -First $numskip2
- Write-Host ""
- Write-Host "**** Starting with Line:"
- Get-Content $hash2 | Select -Index ($numskip2)
- Write-Host
- $continue = Read-Host "Press [ENTER] to accept, any other key to choose again"
- }
- # https://stackoverflow.com/questions/76415338/in-powershell-how-do-i-split-text-from-compare-object-input-and-sort-by-one-of/
- $diff = Compare-Object -ReferenceObject (Get-Content "$hash1" | Select -Skip $numskip1) -DifferenceObject (Get-Content "$hash2" | Select -Skip $numskip2 ) |
- ForEach-Object {
- if ($_.SideIndicator -eq "<=") { $_.SideIndicator = "($((Get-ChildItem $hash1).Name))" } elseif ($_.SideIndicator -eq "=>") { $_.SideIndicator = "($((Get-ChildItem $hash2).Name))" }
- $_
- } |
- Group-Object { $_.InputObject -replace '^.+ ' } |
- ForEach-Object {
- $_.Group | Format-Table -HideTableHeaders |
- Out-String | ForEach-Object TrimEnd
- }
- if ($diff) {
- Write-output "**** $($datetime) '$($hash1)' vs '$($hash2)'`n**** Matching file path/names with mismatched hashes are grouped together`n**** Individual file names means unique file/hash in that log file" $diff > "$hclog"
- } else {
- Write-Output "**** $($datetime) '$($hash1)' vs '$($hash2)' `n`n**** ALL CLEAR! NO DIFFERENCES FOUND!" > "$hclog"
- }
- Write-Host ""
- Write-Host "**** Results stored in $($pwd)\$($hclog)"
- Write-Host ""
- }
- Do { $choice = Read-Host "[G]enerate Hash or [C]ompare Hash Logs" } until ($choice -in 'g','c')
- If ( $choice -eq "g" ) { GenerateHash }
- If ( $choice -eq "c" ) { CompareHash }
- cmd /c 'pause'
Add Comment
Please, Sign In to add comment