HTWingNut

Powershell Generate MD5 Hash and Compare Hash

Jun 6th, 2023 (edited)
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | Software | 0 0
  1. # generatemd5.ps1 by HTWingNut 06 June 2023
  2. Clear-Host
  3. Write-Host ""
  4. $datetime = Get-Date
  5. $timestamp = $datetime.ToString("yyyyMMdd_HHmmss")
  6. $reqpath = 'Input path to generate md5 hashes'
  7. $basepath = $hash1 = $hash2 = "?"
  8. $choice=$null
  9. $md5log = 'hashes_'+$timestamp+'.md5'
  10.  
  11. function GenerateHash {
  12.  
  13. Clear-Host
  14. Write-Host ""
  15. Write-Host "Generate Hash Files ..."
  16. Write-Host ""
  17.  
  18. while (!(Test-Path -Path $basepath)) { $basepath = Read-Host -Prompt $reqpath; if ( $basepath -eq "") { $basepath="?" } }
  19. $numfiles = ( Get-ChildItem -Path "$basepath" -Recurse -File | Measure-Object ).Count
  20.  
  21. $lchar = $basepath.Substring($basepath.length-1,1)
  22. If ($lchar -eq "\") { $basepath = $basepath.Substring(0,$basepath.length-1) }
  23.  
  24. Write-Host "Total Files: $numfiles"
  25. Write-Host ""
  26. $count=1
  27.  
  28. Write-Output "**** $($datetime) '$($basepath)'" | Set-Content "$md5log"
  29.  
  30. Get-ChildItem -Path "$basepath" -Recurse -File |
  31. Get-FileHash -Algorithm MD5 |
  32. Select-Object Hash,Path |
  33. ForEach-Object {
  34. $_.Path = ($_.Path -replace [regex]::Escape("$basepath"), '')
  35. Write-Host "$($count) of $($numfiles)" $_.Hash $_.Path
  36. ++$count
  37. $_
  38. } |
  39. Format-Table -AutoSize -HideTableHeaders |
  40. Out-String -Width 4096 |
  41. Out-File "$md5log" -encoding ASCII -append
  42.  
  43. (get-content $md5log).Trim() -ne '' | Set-Content $md5log
  44.  
  45. Write-Host ""
  46. Write-Host "Hashes stored in file '$pwd\$md5log'"
  47. Write-Host ""
  48.  
  49. }
  50.  
  51. function CompareHash {
  52.  
  53. $continue = "n"
  54.  
  55. While ([bool]$continue) {
  56.  
  57. Clear-Host
  58. Write-Host ""
  59. Write-Host "Compare Hash Files..."
  60.  
  61. $hash1 = $hash2 = "?"
  62. $numksip1 = $numskip2 = "0"
  63.  
  64. Write-Host ""
  65. while (!(Test-Path $hash1)) { $hash1 = Read-Host -Prompt "Enter Path/Name for Log 1"; if ( $hash1 -eq "") { $hash1="?" } }
  66. Write-Host ""
  67. Write-Host "First 9 lines of $($hash1):"
  68. $i=1; Get-Content $hash1 | Select -First 9 | % {"$($i) $_"; $i++}
  69. Write-Host ""
  70. 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)
  71. Write-Host ""
  72.  
  73. while (!(Test-Path $hash2)) { $hash2 = Read-Host -Prompt "Enter Path/Name for Log 2"; if ( $hash2 -eq "") { $hash2="?" } }
  74. Write-Host ""
  75. Write-Host "First 9 lines of $($hash2):"
  76. $i=1; Get-Content $hash2 | Select -First 9 | % {"$($i) $_"; $i++}
  77. Write-Host ""
  78. 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)
  79.  
  80. $hclog = "HashCompare_$(((Get-Item $hash1).Basename).Replace(' ','_'))_vs_$(((Get-Item $hash2).Basename).Replace(' ','_'))_$timestamp.txt"
  81.  
  82. Write-Host ""
  83. Write-Host "---------------------"
  84. Write-Host ""
  85.  
  86. Write-Host "**** File: '$((Get-Item $hash1).Name)'"
  87. Write-Host "**** Skipping Lines:"
  88. Get-Content $hash1 | Select -First $numskip1
  89. Write-Host ""
  90. Write-Host "**** Starting with Line:"
  91. Get-Content $hash1 | Select -Index ($numskip1)
  92. Write-Host ""
  93. Write-Host "---------------------"
  94. Write-Host ""
  95. Write-Host "**** File: '$((Get-Item $hash2).Name)'"
  96. Write-Host "**** Skipping Lines:"
  97. Get-Content $hash2 | Select -First $numskip2
  98. Write-Host ""
  99. Write-Host "**** Starting with Line:"
  100.  
  101. Get-Content $hash2 | Select -Index ($numskip2)
  102. Write-Host
  103. $continue = Read-Host "Press [ENTER] to accept, any other key to choose again"
  104.  
  105. }
  106.  
  107. # https://stackoverflow.com/questions/76415338/in-powershell-how-do-i-split-text-from-compare-object-input-and-sort-by-one-of/
  108.  
  109. $diff = Compare-Object -ReferenceObject (Get-Content "$hash1" | Select -Skip $numskip1) -DifferenceObject (Get-Content "$hash2" | Select -Skip $numskip2 ) |
  110. ForEach-Object {
  111. if ($_.SideIndicator -eq "<=") { $_.SideIndicator = "($((Get-ChildItem $hash1).Name))" } elseif ($_.SideIndicator -eq "=>") { $_.SideIndicator = "($((Get-ChildItem $hash2).Name))" }
  112. $_
  113. } |
  114. Group-Object { $_.InputObject -replace '^.+ ' } |
  115. ForEach-Object {
  116. $_.Group | Format-Table -HideTableHeaders |
  117. Out-String | ForEach-Object TrimEnd
  118. }
  119.  
  120. if ($diff) {
  121. 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"
  122. } else {
  123. Write-Output "**** $($datetime) '$($hash1)' vs '$($hash2)' `n`n**** ALL CLEAR! NO DIFFERENCES FOUND!" > "$hclog"
  124. }
  125.  
  126. Write-Host ""
  127. Write-Host "**** Results stored in $($pwd)\$($hclog)"
  128. Write-Host ""
  129.  
  130.  
  131. }
  132.  
  133. Do { $choice = Read-Host "[G]enerate Hash or [C]ompare Hash Logs" } until ($choice -in 'g','c')
  134. If ( $choice -eq "g" ) { GenerateHash }
  135. If ( $choice -eq "c" ) { CompareHash }
  136.  
  137. cmd /c 'pause'
Add Comment
Please, Sign In to add comment