PowerShell script to check my backup by comparing file hashes

This sample PowerShell script compares the files in two different paths by calculating and comparing hashes for each file. I use it to compare my backup to the source data, after the backup is complete.

 

 $SourcePath = "D:\"
$BackupPath = "F:\Backup 2018-05-27\"

Write-Progress -Activity "Getting File List" -PercentComplete 0
$FileList = DIR $SourcePath -Recurse -File

$Total = $FileList.Count
$Count = 0
$BadCount = 0
$FileList | % {

  $File = $_.FullName
  $Backup = $File.Replace($SourcePath, $BackupPath)

  Try { 
    $Match = (Get-FileHash $File).Hash -eq (Get-FileHash $Backup).Hash 
  } 
  Catch { 
    $Match = $false 
  }

  If (-not $Match) {
    $BadCount++
    "Hash mismatch: $File, $Backup"
  }

  $Count++
  If ($Count % 1000 -eq 0) {
    Write-Progress -Activity "File $Count of $Total" -PercentComplete ($Count/$Total*100) 
  }
}
Write-Progress -Activity "Checking Files" -Completed
"There were $BadCount bad files out of the $Count files checked"

Comments

  • Anonymous
    November 26, 2018
    Thanks, I was able to take this and create a script to compare two directories and their children then update or create a file at a backup destination. I was able to make some efficiency improvements and It's able to loop through ~22,000 files in half the time. I'd like to share but I'm new to this and am not sure where I should do that.