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 (0)

Skip to main content