Search notes:

PowerShell cmdLet Compare-Object

Comparing two string arrays

The following example tries to demonstrate how compare-object can be used to compare two string arrays:
$words_1 =        'foo', 'bar', 'baz', 'four'
$words_2 = 'one', 'foo', 'bar', 'baz'

compare-object $words_1  $words_2
#
#  InputObject SideIndicator
#  ----------- -------------
#  one         =>
#  four        <=
Github repository about-PowerShell, path: /cmdlets/object/compare/string-arrays.ps1

Comparing directory structures

A combination of compare-object and get-childItem -recurse allows to compare directory structures.
The following command finds directories or files that are missing below dir-one or dir-two that are present in the other directory.
compare-object (get-childItem -recurse dir-one) (get-childItem -recurse dir-two)
Github repository about-PowerShell, path: /cmdlets/object/compare/compare-directory-structure.ps1
Note: this command only checks if the directory structure is equal. If a file that is present in both directories has a different content, it won't be reported!

Comparing two psObjects

The following script compares two psObjects.
These objects differ one from another in two ways:
Although these two objects are clearly different, compare-object won't report them as such.
With -includeEqual, they are explicitly shown to be equal:
$psObject_1 = new-object psobject -property @{ word_1 = 'abc' ; word_2 = 'def' ; word_3 = 'ghi' ; word_4 = 'jkl' }
$psObject_2 = new-object psobject -property @{ word_X = 'abc' ; word_2 = 'def' ; word_3 = 'XXX' ; word_4 = 'jkl' }

compare-object $psObject_1 $psObject_2


compare-object $psObject_1 $psObject_2 -includeEqual
#
# InputObject                                       SideIndicator
# -----------                                       -------------
# @{word_2=def; word_4=jkl; word_1=abc; word_3=ghi} ==
Github repository about-PowerShell, path: /cmdlets/object/compare/psObjects.ps1

Alias

An alias for compare-object is diff

See also

Powershell command noun: object

Index