PowerShell Logo Small

Compare-Object



This is the built-in help made by Microsoft for the command 'Compare-Object', in PowerShell version 5 - as retrieved from Windows version 'Microsoft Windows Server 2012 R2 Standard' PowerShell help files on 2016-06-23.

For PowerShell version 3 and up, where you have Update-Help, this command was run just before creating the web pages from the help files.

SYNOPSIS

Compares two sets of objects.

SYNTAX


Compare-Object [-ReferenceObject] <PSObject[]> [-DifferenceObject] <PSObject[]> [-CaseSensitive] [-Culture [<String>]] [-ExcludeDifferent] [-IncludeEqual]
[-InformationAction {SilentlyContinue | Stop | Continue | Inquire | Ignore | Suspend}] [-InformationVariable [<System.String>]] [-PassThru] [-Property [<Object[]>]]
[-SyncWindow [<Int32>]] [<CommonParameters>]



Search powershellhelp.space

DESCRIPTION


The Compare-Object cmdlet compares two sets of objects. One set of objects is the "reference set," and the other set is the "difference set."


The result of the comparison indicates whether a property value appeared only in the object from the reference set (indicated by the <= symbol), only in the object from the
difference set (indicated by the => symbol) or, if the IncludeEqual parameter is specified, in both objects (indicated by the == symbol).


NOTE: If the reference set or the difference set is null ($null), Compare-Object generates a terminating error.



<

RELATED LINKS

Online Version: http://go.microsoft.com/fwlink/p/?linkid=293945
ForEach-Object
Group-Object
Measure-Object
New-Object
Select-Object
Sort-Object
Tee-Object
Where-Object

REMARKS

<

Examples


-------------------------- EXAMPLE 1 --------------------------

PS C:\>compare-object -referenceobject $(get-content C:\test\testfile1.txt) -differenceobject $(get-content C:\test\testfile2.txt)



This command compares the contents of two text files. It displays only the lines that appear in one file or in the other file, not lines that appear in both files.










-------------------------- EXAMPLE 2 --------------------------

PS C:\>compare-object -referenceobject $(get-content C:\Test\testfile1.txt) -differenceobject $(get-content C:\Test\testfile2.txt) -includeequal



This command compares each line of content in two text files. It displays all lines of content from both files, indicating whether each line appears in only Textfile1.txt or
Textfile2.txt or whether each line appears in both files.










-------------------------- EXAMPLE 3 --------------------------

PS C:\>$processes_before = get-process
PS C:\>notepad
PS C:\>$processes_after = get-process
PS C:\>compare-object -referenceobject $processes_before -differenceobject $processes_after



These commands compare two sets of process objects.

The first command uses the Get-Process cmdlet to get the processes on the computer. It stores them in the $processes_before variable.

The second command starts Notepad.

The third command uses the Get-Process cmdlet again and stores the resulting processes in the $processes_after variable.

The fourth command uses the Compare-Object cmdlet to compare the two sets of process objects. It displaysthe differences between them, which include the new instance of
Notepad.