You are currently viewing PowerShell Compare-Object Cmdlet

PowerShell Compare-Object Cmdlet

PowerShell Compare-Object Cmdlet

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed Tee-Object cmdlet in PowerShell.

In this blog post, we will discuss Compare-Object cmdlet in PowerShell. You can use Compare-Object cmdlet to compare two objects where one set of objects is the source or reference and another set of objects is the target or difference. The Compare-Object cmdlet tries to compare the objects based on any comparison methods it finds on the object. If no comparison object is found it performs a string comparison by converting the object into a string. You can also pass one or more properties for comparison, when properties are passed the comparison is made based on those properties only.

Compare-Object cmdlet output:

  • (<=): property value appeared only in the reference object
  • (=>): property value appeared only in the difference object
  • (==): property value appeared both in the reference object and difference object

Note: If the reference or the difference objects are null ($null), Compare-Object generates a terminating error.

Compare-Object Cmdlet Syntax:

Compare-Object Cmdlet Argument List:

  • CaseSensitive: Indicates that comparisons should be case-sensitive.
  • Culture: Specifies the culture to use for comparisons.
  • DifferenceObject: Specifies the objects that are compared to the reference objects.
  • ExcludeDifferent: Indicates that this cmdlet displays only the characteristics of compared objects that are equal. The differences between the objects are discarded.
  • IncludeEqual: IncludeEqual displays the matches between the reference and difference objects.
  • PassThru: When you use the PassThru parameter, Compare-Object omits the PSCustomObject wrapper around the compared objects and returns the differing objects, unchanged.
  • Property: Specifies an array of properties of the reference and difference objects to compare.
  • ReferenceObject: Specifies an array of objects used as a reference for comparison.
  • SyncWindow: Specifies the number of adjacent objects that Compare-Object inspects while looking for a match in a collection of objects.

PowerShell Compare-Object Cmdlet:

Hope you have enjoyed this article. In the next blog post, we will discuss Add-Content cmdlet in PowerShell.

To get more details on PowerShell, kindly follow below official documentation


Leave a Reply