0
0
PowerShellscripting~5 mins

Compare-Object for differences in PowerShell

Choose your learning style9 modes available
Introduction
Compare-Object helps you find what is different between two sets of things, like lists or files. It shows what is only in one set or the other.
You want to see which files are different between two folders.
You need to find which names are in one list but not in another.
You want to check if two sets of data have any differences.
You want to compare configuration settings from two sources.
You want to quickly spot changes between two versions of a list.
Syntax
PowerShell
Compare-Object -ReferenceObject <Object[]> -DifferenceObject <Object[]> [-Property <string[]>] [-IncludeEqual] [-PassThru]
ReferenceObject is the first list or set you compare from.
DifferenceObject is the second list or set you compare to.
Examples
Compares two lists and shows differences.
PowerShell
Compare-Object -ReferenceObject $list1 -DifferenceObject $list2
Shows both differences and items that are the same.
PowerShell
Compare-Object -ReferenceObject $list1 -DifferenceObject $list2 -IncludeEqual
Outputs only the differing objects without extra info.
PowerShell
Compare-Object -ReferenceObject $list1 -DifferenceObject $list2 -PassThru
Sample Program
This script compares two fruit lists and shows which fruits are unique to each list.
PowerShell
$list1 = @('apple', 'banana', 'cherry')
$list2 = @('banana', 'cherry', 'date')
Compare-Object -ReferenceObject $list1 -DifferenceObject $list2
OutputSuccess
Important Notes
SideIndicator shows where the item is: '<=' means only in ReferenceObject, '=>' means only in DifferenceObject.
If you want to compare complex objects, use the -Property parameter to specify which property to compare.
Use -IncludeEqual if you want to see items that are the same in both lists.
Summary
Compare-Object finds differences between two sets of data.
It shows which items are unique to each set with a clear indicator.
You can customize output with options like -IncludeEqual and -PassThru.