Powershell - Compare-Object Cmdlet



Compare-Object cmdlet can be used to compare two objects.

In these examples, we're see the Compare-Object cmdlet in action.

Example 1

In this example, first we've a file test.txt in D:\temp\test with content "Welcome to TutorialsPoint.Com" and test1.txt with content "Hello World!" and "Welcome to TutorialsPoint.Com" in two lines.

Compare the files. Type the following command in PowerShell ISE Console. Common line(s) will be displayed.

Compare-Object -ReferenceObject $(Get-Content D:\temp\test\test.txt) -DifferenceObject $(Get-Content D:\temp\test\test1.txt)

Output

You can see following output in PowerShell console.

InputObject                        SideIndicator                                  
-----------                        -------------                                  
Hello World!                       => 

Example 2

Compare the content of files. Type the following command in PowerShell ISE Console. All line(s) with indicator will be displayed.

Type the following command in PowerShell ISE Console

Compare-Object -ReferenceObject $(Get-Content D:\temp\test\test.txt) -DifferenceObject $(Get-Content D:\temp\test\test1.txt) -IncludeEqual

Output

You can see following output in PowerShell console.

InputObject                                      SideIndicator                                  
-----------                                      -------------                                  
Welcome to TutorialsPoint.Com                    ==                                             
Hello World!                                     =>              
powershell_advanced_cmdlets.htm
Advertisements