Search code examples
powershellhashcompareobject

Powershell Compare-Object with Hash only pass $_.SideIndicator and not $_.path $_.hash


I am trying to compare two folders at the hash level.

I get the hash information; Algorithm, Hash, Path $SourceDocs and $DestDocs both have the Algorithm, Hash, Path

When I run Compare-Object, the only value that gets passed along is SideIndicator

I had this running without the Hash, but files with similar names but different would pass as ok

cls

$Folder1 = "E:\Backups\Offsite"
$Folder2 = "E:\s3-drive\Backup\Compare"
$Folder3 = "E:\Backups\Temp_transfer"

Write-host 
$time = Get-Date -Format HH:mm
Write-host $time
Write-host $Folder1
Write-host $Folder2
Write-host $Folder3
Write-host 

Write-host 
$time = Get-Date -Format HH:mm
Write-host $time
Write-host "Getting Directory Hash: " $Folder1
$SourceDocs = Get-ChildItem –Path $Folder1 | foreach  {Get-FileHash –Path $_.FullName -Algorithm MD5}
Write-host 
Write-host "Results for: " $Folder1
$SourceDocs
Write-host 

Write-host 
$time = Get-Date -Format HH:mm
Write-host $time
Write-host "Getting Directory Hash: " $Folder2
$DestDocs = Get-ChildItem –Path $Folder2 | foreach  {Get-FileHash –Path $_.FullName -Algorithm MD5}
Write-host 
Write-host 
Write-host "Results for: " $Folder2
$DestDocs
Write-host 

Write-host 
$time = Get-Date -Format HH:mm
Write-host $time
Write-host "Comparing..."
Compare-Object -ReferenceObject $SourceDocs.Hash  -DifferenceObject $DestDocs.Hash -IncludeEqual -Property path, hash, SideIndicator -PassThru

Write-host 
$time = Get-Date -Format HH:mm
Write-host $time
Write-Host "These Files do not require action"  -ForegroundColor Yellow
Compare-Object -ReferenceObject $SourceDocs.Hash -DifferenceObject $DestDocs.Hash -IncludeEqual -Property path, hash, SideIndicator -PassThru |
    Where-Object { $_.SideIndicator -eq '==' } | Select-Object -Property path, hash, SideIndicator |
    foreach-object -process{
    Write-Host $_.path $_.hash $_.SideIndicator}


Write-host 
$time = Get-Date -Format HH:mm
Write-Host "Duplicate Files to"  -ForegroundColor Yellow -NoNewline; Write-Host " " $Folder1 -ForegroundColor Cyan 
Compare-Object -ReferenceObject $SourceDocs.Hash -DifferenceObject $DestDocs.Hash -IncludeEqual -Property path, hash, SideIndicator -PassThru |
    Where-Object { $_.SideIndicator -eq '<=' } | Select-Object -Property path, hash, SideIndicator |
    foreach-object -process{
    Write-Host $_.path $_.hash $_.SideIndicator}


Write-host 
$time = Get-Date -Format HH:mm
Write-Host "Remove Files From"  -ForegroundColor Yellow -NoNewline; Write-Host " " $Folder2 -ForegroundColor Cyan 
Compare-Object -ReferenceObject $SourceDocs.Hash -DifferenceObject $DestDocs.Hash -IncludeEqual -Property path, hash, SideIndicator -PassThru |
    Where-Object { $_.SideIndicator -eq '=>' } | Select-Object -Property path, hash, SideIndicator |
    foreach-object -process{
    Write-Host $_.path $_.hash $_.SideIndicator}

Break

And this is the output

11:56
E:\Backups\Offsite
E:\s3-drive\Backup\Compare
E:\Backups\Temp_transfer


11:56
Getting Directory Hash:  E:\Backups\Offsite

Results for:  E:\Backups\Offsite

Algorithm       Hash                                                                   Path                           
---------       ----                                                                   ----                           
MD5             85D3D722185CC02750485DF0E0A7F1B4                                       E:\Backups\Offsite\Offsite_f...
MD5             41BFAAE4B1010D0FD09C669A4707ACF6                                       E:\Backups\Offsite\Offsite_f...
MD5             92641097960F47591B716282D61F7D3C                                       E:\Backups\Offsite\Offsite_f...
MD5             B48374310B920AE3F4C3F26DBC7564B5                                       E:\Backups\Offsite\Offsite_f...
MD5             D41D8CD98F00B204E9800998ECF8427E                                       E:\Backups\Offsite\Offsite_f...
MD5             D41D8CD98F00B204E9800998ECF8427E                                       E:\Backups\Offsite\Offsite_f...
MD5             24DF814C247EDEED55DBF875B2BAE85B                                       E:\Backups\Offsite\Offsite_f...
MD5             4BA69B5F53C40C48C3C032E883CC12EB                                       E:\Backups\Offsite\Offsite_f...


12:14
Getting Directory Hash:  E:\s3-drive\Backup\Compare


Results for:  E:\s3-drive\Backup\Compare
MD5             85D3D722185CC02750485DF0E0A7F1B4                                       E:\s3-drive\Backup\Compare...
MD5             92641097960F47591B716282D61F7D3C                                       E:\s3-drive\Backup\Compare...
MD5             B48374310B920AE3F4C3F26DBC7564B5                                       E:\s3-drive\Backup\Compare...
MD5             24DF814C247EDEED55DBF875B2BAE85B                                       E:\s3-drive\Backup\Compare...
MD5             D41D8CD98F00B204E9800998ECF8427E                                       E:\s3-drive\Backup\Compare...
MD5             0627B4727E2BFE1D1CB7F06B82BFCC5C                                       E:\s3-drive\Backup\Compare...
MD5             4BA69B5F53C40C48C3C032E883CC12EB                                       E:\s3-drive\Backup\Compare...


12:27
Comparing...
85D3D722185CC02750485DF0E0A7F1B4
41BFAAE4B1010D0FD09C669A4707ACF6
92641097960F47591B716282D61F7D3C
B48374310B920AE3F4C3F26DBC7564B5
D41D8CD98F00B204E9800998ECF8427E
D41D8CD98F00B204E9800998ECF8427E
24DF814C247EDEED55DBF875B2BAE85B
4BA69B5F53C40C48C3C032E883CC12EB

12:27
These Files do not require action
  ==
  ==
  ==
  ==
  ==
  ==
  ==

Copy Files to  E:\Backups\Offsite
  <=

Remove Files From  E:\s3-drive\Backup\Compare
  =>

The whole idea here is to compare the two folders and take one of 3 options:

  • == Nothing
  • <= Copy
  • => Delete

Any insight would be appreciated

Update...

The Get-FileHash now looks like this:

$SourceDocs = Get-ChildItem -path $Folder1 -File | foreach  {
    Get-FileHash –Path $_.FullName -Algorithm MD5
    New-Object PSCustomObject -Property @{
    "DisplayName" = (Split-Path $_.FullName -Leaf -Resolve)}}

for some reason i could not get the name only to show so i used the New-Object and forced it

and the compare statement had to be adjusted too:

$compare = Compare-Object -ReferenceObject $SourceDocs -DifferenceObject $DestDocs -Property Hash -IncludeEqual -PassThru | Select-Object Hash, SideIndicator, Path

to

$compare = Compare-Object -ReferenceObject $SourceDocs -DifferenceObject $DestDocs -Property Hash -IncludeEqual -PassThru | Select-Object Hash, SideIndicator, DisplayName

Solution

  • UPDATE

    Taken from the part where you are comparing, I think this will give you the wanted output:

    Write-host "Comparing..."
    $compare = Compare-Object -ReferenceObject $SourceDocs -DifferenceObject $DestDocs -Property Hash -IncludeEqual -PassThru | Select-Object Hash, SideIndicator, Path
    Write-host
    
    $doNothing = $compare | Where-Object { $_.SideIndicator -eq '==' }
    if ($doNothing) {
        Write-Host "These Files do not require action" -ForegroundColor Yellow
        $doNothing | Format-Table -AutoSize
    }
    
    $doCopy = $compare | Where-Object { $_.SideIndicator -eq '<=' }
    if ($doCopy) {
        Write-Host "Copy these files"  -ForegroundColor Cyan
        $doCopy | Format-Table -AutoSize
    }
    
    $doDelete = $compare | Where-Object { $_.SideIndicator -eq '=>' }
    if ($doDelete) {
        Write-Host "Delete these files"  -ForegroundColor Magenta
        $doDelete | Format-Table -AutoSize
    }
    

    And to answer the question in the title: In your Compare-Object statement, you set it to compare only the Hash properties where you should compare the full objects $SourceDocs and $DestDocs