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 <=
Comparing 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)
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
These objects differ one from another in two ways:
- Attribute names (
$psObject_1
has the attribute word_1
while $psObject_2
has the attribute word_x
)
- Attribute values (The value of
.word_3
is ghi
or XXX
, respectively)
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} ==