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.
https://cloudaffaire.com/powershell-tee-object-cmdlet/
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:
1 2 3 4 5 6 7 8 9 10 11 |
## Compare-Object ## [-ReferenceObject] ## [-DifferenceObject] ## [-SyncWindow ## [-Property ## [-ExcludeDifferent] ## [-IncludeEqual] ## [-PassThru] ## [-Culture ## [-CaseSensitive] ## [ |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
########################################## ## PowerShell | Cmdlet | Compare-Object ## ########################################## ## PowerShell Latest Version (5) ## declare two arrays with string data type $a = "cat", "rat", "dog", "man" $b = "apple", "banana", "Dog", "mango" ## compare two objects in PowerShell Compare-Object -ReferenceObject $a -DifferenceObject $b ## compare two objects in PowerShell with case sensitive Compare-Object -ReferenceObject $a -DifferenceObject $b -CaseSensitive ## compare two objects in PowerShell and show the difference and similarity Compare-Object -ReferenceObject $a -DifferenceObject $b -IncludeEqual ## compare two objects in PowerShell and show what is common Compare-Object -ReferenceObject $a -DifferenceObject $b -IncludeEqual -ExcludeDifferent ## compare two objects in PowerShell based on specific object property Compare-Object -ReferenceObject $a -DifferenceObject $b -Property length |
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