Challenge - 5 Problems
Compare-Object Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
What is the output of Compare-Object with default SideIndicator?
Given these two arrays:
What is the output?
$a = 1,2,3,4
$b = 3,4,5,6
Compare-Object $a $b
What is the output?
PowerShell
$a = 1,2,3,4 $b = 3,4,5,6 Compare-Object $a $b
Attempts:
2 left
💡 Hint
SideIndicator <= means the item is only in the first array; >= means only in the second.
✗ Incorrect
Compare-Object compares two collections and shows differences. Items only in $a have SideIndicator <=, items only in $b have >=. Here, 1 and 2 are only in $a, 5 and 6 only in $b.
💻 Command Output
intermediate2:00remaining
What does -IncludeEqual parameter do in Compare-Object?
Given:
What output will this produce?
$a = 'apple','banana','cherry'
$b = 'banana','cherry','date'
Compare-Object $a $b -IncludeEqual
What output will this produce?
PowerShell
$a = 'apple','banana','cherry' $b = 'banana','cherry','date' Compare-Object $a $b -IncludeEqual
Attempts:
2 left
💡 Hint
SideIndicator == means the item is in both collections.
✗ Incorrect
With -IncludeEqual, Compare-Object shows items that are the same in both collections with SideIndicator ==. Items unique to $a have <=, unique to $b have >=.
📝 Syntax
advanced2:00remaining
Which Compare-Object command correctly compares two custom objects by a property?
You have two arrays of objects:
Which command compares them by the 'Name' property only?
$a = @(@{Name='John'; Age=30}, @{Name='Jane'; Age=25})
$b = @(@{Name='John'; Age=31}, @{Name='Jake'; Age=25})Which command compares them by the 'Name' property only?
Attempts:
2 left
💡 Hint
Use -Property to specify which property to compare.
✗ Incorrect
The -Property parameter tells Compare-Object to compare objects based on that property only. Here, comparing by 'Name' shows differences ignoring 'Age'.
💻 Command Output
advanced2:00remaining
What is the output count of Compare-Object when comparing identical arrays without -IncludeEqual?
Given:
How many objects does this command output?
$a = 1,2,3
$b = 1,2,3
Compare-Object $a $b
How many objects does this command output?
PowerShell
$a = 1,2,3 $b = 1,2,3 Compare-Object $a $b
Attempts:
2 left
💡 Hint
Without -IncludeEqual, identical items are not shown.
✗ Incorrect
Compare-Object without -IncludeEqual only shows differences. Since arrays are identical, no differences exist, so output count is zero.
🚀 Application
expert3:00remaining
How to find items only in the second array using Compare-Object?
You want to get only the items that exist in $b but not in $a.
Which command correctly outputs only 'yellow' and 'black'?
$a = 'red','green','blue'
$b = 'green','blue','yellow','black'
Which command correctly outputs only 'yellow' and 'black'?
PowerShell
$a = 'red','green','blue' $b = 'green','blue','yellow','black'
Attempts:
2 left
💡 Hint
SideIndicator '=>' means item is only in the second collection passed to Compare-Object.
✗ Incorrect
Compare-Object $a $b outputs items unique to $a with '<=' and unique to $b with '=>'. Filtering SideIndicator '=>' gives items only in $b.