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 ReferenceObject (the collection on the left) has the values, but the DifferenceObject (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 that 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 that 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 objects:

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 

By default, Compare-Object will write an error if either the reference or difference objects are null. If Compare-Object is used when there is a chance of either being empty, the following technique can be used to avoid an error being generated provided neither contains an explicit null value:

$reference = Get-ChildItem C:WindowsSystem32	cpmon*.ini
$difference = Get-ChildItem C:WindowsSysWOW64 cpmon*.ini
Compare-Object @($reference) @($difference) -Property Name

The array, (@()), wrapping each parameter value will be discarded by PowerShell. If $difference is empty, it will be treated as an empty array instead of it being a null value.

..................Content has been hidden....................

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