Comparing

The Compare-Object command allows collections of objects to be compared to one another.

Compare-Object must be supplied with a value for the ReferenceObject and DifferenceObject parameters, which are normally collections or arrays of objects. If both values are equal, Compare-Object does not return anything by default. For example, both the Reference and Difference object in the following example are identical:

Compare-Object -ReferenceObject 1, 2 -DifferenceObject 1, 2 

If there are differences, Compare-Object will display the results, as shown here:

PS> Compare-Object -ReferenceObject 1, 2, 3, 4 -DifferenceObject 1, 2

InputObject SideIndicator
----------- -------------
3 <=
4 <=

This shows that the Reference object (the collection on the left) has the values, but the Difference object (the collection on the right) does not.

Compare-Object has a number of other parameters that may be used to change the output. The IncludeEqual parameter adds values which are present in both collections to the output:

PS> Compare-Object -ReferenceObject 1, 2, 3, 4 -DifferenceObject 1, 2 -IncludeEqual

InputObject SideIndicator
----------- -------------
1 ==
2 ==
3 <=
4 <=

ExcludeDifferent will omit the results which differ. This parameter makes sense if IncludeEqual is also set; without this, the command will always return nothing.

The PassThru parameter is used to return the original object instead of the representation showing the differences. In the following example, it is used to select values that are common to both the reference and difference object:

PS> Compare-Object -ReferenceObject 1, 2, 3, 4 -DifferenceObject 1, 2 -ExcludeDifferent -IncludeEqual -PassThru
1
2

Compare-Object is able to compare based on properties of objects as well as the simpler values in the preceding examples. This can be a single property, or a list of properties. For example, the following command compares the content of C:WindowsSystem32 with C:WindowsSysWOW64, returning files that have the same name and are the same size in both:

$reference = Get-ChildItem C:WindowsSystem32 -File 
$difference = Get-ChildItem C:WindowsSysWOW64 -File 
Compare-Object $reference $difference -Property Name, Length -IncludeEqual -ExcludeDifferent 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.213.238