To compare two folders I perform the following steps:
- Use the Get-ChildItem cmdlet with the recurse switched parameter and the path parameter (points to the folder to use for reference) to obtain a collection of fileinfo objects. Store these objects in a variable.
- Use the Get-ChildItem cmdlet with the recurse switched parameter and the path parameter (points to the folder to use for comparison) to obtain a collection of fileinfo objects. Store these objects in a different variable.
- Use the Compare-Object cmdlet and specify the objects stored in the first variable to the ReferenceObject parameter. Supply the objects stored in the second variable to the DifferenceObject parameter.
Note Do not get hung up on whether the first folder should be the reference object or the difference object. The position of the folder in the two parameters determines the direction of the comparison arrows, but as long as you know which folder is difference or reference, you will be fine.
The code I type on my laptop is shown here:
$fso = Get-ChildItem -Recurse -path C:\fso
$fsoBU = Get-ChildItem -Recurse -path C:\fso_BackUp
Compare-Object -ReferenceObject $fso -DifferenceObject $fsoBU
The code and associated output are shown in the following figure. The output tells me that inputobject (this is the difference object parameter) is missing three files: a.txt, b.txt, and c.txt. I need to copy these three files to the c:\fso_backup folder.
Basic comparison of two folders and write a log file listing the differences.
ROBOCOPY “\\FileShare\SourceFolder” “\\FileShare\ComparisonFolder” /e /l /ns /njs /njh /ndl /fp /log:reconcile.txt