How to use Compare-Object in PowerShell?


Compare-Object command in PowerShell is used to compare two objects. Objects can be a variable content, two files, strings, etc. This cmdlet uses few syntaxes to show the difference between objects which is called side indicators.

=> - Difference in destination object.
<= - Difference in reference (source) object.
== - When the source and destination objects are equal.

Example1: Comparison of two strings.

PS C:\> Compare-Object "World" "Alpha"

InputObject SideIndicator
----------- -------------
Alpha =>
World <=

In the above example, the Alpha string shows the right indicator means it is the difference from the source object while the World string shows the left indicator means it is different from the destination string.

Example2: Comparison of two equal strings.

The below example won’t show any output because the source and destination reference objects are the same but when you use -IncludeEqual parameter, it will show the equal indicator (==) for the matching object

PS C:\> Compare-Object "World" "woRld"

PS C:\> Compare-Object "World" "woRld" -IncludeEqual

InputObject SideIndicator
----------- -------------
World ==

Please note, Comparison-Object is not case sensitive. For the case-sensitive comparison, use -CaseSensitive parameter.

PS C:\> Compare-Object "World" "woRld" -CaseSensitive


InputObject SideIndicator
----------- -------------
woRld =>
World <=

Example3: Source and Destination file comparison.

$sourcefiles = Get-ChildItem C:\Test1 -Recurse
$destfiles = Get-ChildItem C:\Test2\ -Recurse
Compare-Object $sourcefiles $destfiles -IncludeEqual

Output

InputObject SideIndicator
----------- -------------
File1.txt ==
File3.txt =>
File2.txt <=

The above example shows that the file1.txt exists at both locations, while File3.txt is at the destination location but not at the source location and File2.txt exists at the source location but not at the destination location.

If we use -ExcludeDifference parameter, the output won’t be displayed unless we add -IncludeEqual parameter.

Compare-Object $sourcefiles $destfiles -ExcludeDifferent

The below command will display only the files which are matching.

PS C:\> Compare-Object $sourcefiles $destfiles -IncludeEqual -ExcludeDifferent

InputObject SideIndicator
----------- -------------
File1.txt ==

Examples 4: Comparison with the Property name.

To compare two objects with the specific property name, use -Property parameter. In the below example, we will compare files LastWriteTime.

Example

$sfiles = Get-ChildItem C:\Test1\ -Recurse
$dfiles = Get-ChildItem C:\Test2\ -Recurse

Compare-Object $sfiles $dfiles -Property LastWriteTime -IncludeEqual

Output

LastWriteTime SideIndicator
------------- -------------
8/28/2020 7:27:11 AM ==
8/28/2020 7:29:00 AM =>
8/28/2020 7:49:37 AM <=

If you need the name of any specific property in the output then add that property first and then add the property name to compare. For example,

Example

Compare-Object $sfiles $dfiles -Property Name, LastWriteTime -IncludeEqual

Output

Name LastWriteTime SideIndicator
---- ------------- -------------
File1.txt 8/28/2020 7:27:11 AM ==
File3.txt 8/28/2020 7:29:00 AM =>
File2.txt 8/28/2020 7:49:37 AM <=

Updated on: 11-Nov-2020

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements