top of page

PowerShell Compare-Object

<#

The use of the compare-object cmdlet comes in hand when comparing data.

I have two files both consisting of a list of user accounts. 

Of these two files I set the masterlist stored in var $ReferenceFile holds all accounts.

The other file is a subset list of accounts stored in var $DifferenceFile

#>
# Test Data From Files.
$ReferenceFile = Get-content C:\temp\Reference.txt
$DifferenceFile = Get-Content C:\temp\Difference.txt


#This Compare Shows items in  $ReferenceFile (Master List) that are not listed in $DifferenceFile (Subset of Master List)
Compare-Object -ReferenceObject $ReferenceFile -DifferenceObject $DifferenceFile | ? {$_.SideIndicator -eq '<='} | Select -ExpandProperty inputobject


#Compare the two and display items found in both lists
Compare-Object -ReferenceObject $ReferenceFile -DifferenceObject $DifferenceFile -IncludeEqual -ExcludeDifferent | Select -ExpandProperty inputobject


#This Compare Shows items in  $DifferenceFile (Subset of Master List) that are not listed in the $ReferenceFile (Master List)
Compare-Object -ReferenceObject $ReferenceFile -DifferenceObject $DifferenceFile | ? {$_.SideIndicator -eq '=>'} | Select -ExpandProperty inputobject 

bottom of page