0
0
PowerShellscripting~20 mins

Compare-Object for differences in PowerShell - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Compare-Object Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of Compare-Object with default SideIndicator?
Given these two arrays:
$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
A
@{InputObject=1; SideIndicator=>=}
@{InputObject=2; SideIndicator=>=}
@{InputObject=5; SideIndicator=<=}
@{InputObject=6; SideIndicator=<=}
B
@{InputObject=1; SideIndicator=<=}
@{InputObject=2; SideIndicator=<=}
@{InputObject=5; SideIndicator=>=}
@{InputObject=6; SideIndicator=>=}
C
@{InputObject=3; SideIndicator=<=}
@{InputObject=4; SideIndicator=<=}
@{InputObject=5; SideIndicator=>=}
@{InputObject=6; SideIndicator=>=}
D
@{InputObject=1; SideIndicator=<=}
@{InputObject=2; SideIndicator=<=}
@{InputObject=3; SideIndicator=>=}
@{InputObject=4; SideIndicator=>=}
Attempts:
2 left
💡 Hint
SideIndicator <= means the item is only in the first array; >= means only in the second.
💻 Command Output
intermediate
2:00remaining
What does -IncludeEqual parameter do in Compare-Object?
Given:
$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
A
@{InputObject=apple; SideIndicator=&lt;=}
@{InputObject=banana; SideIndicator==}
@{InputObject=cherry; SideIndicator==}
@{InputObject=date; SideIndicator=&gt;=}
B
@{InputObject=apple; SideIndicator=&lt;=}
@{InputObject=banana; SideIndicator=&lt;=}
@{InputObject=cherry; SideIndicator=&lt;=}
@{InputObject=date; SideIndicator=&gt;=}
C
@{InputObject=apple; SideIndicator=&gt;=}
@{InputObject=banana; SideIndicator=&lt;=}
@{InputObject=cherry; SideIndicator=&lt;=}
@{InputObject=date; SideIndicator=&gt;=}
D
@{InputObject=apple; SideIndicator==}
@{InputObject=banana; SideIndicator==}
@{InputObject=cherry; SideIndicator==}
@{InputObject=date; SideIndicator==}
Attempts:
2 left
💡 Hint
SideIndicator == means the item is in both collections.
📝 Syntax
advanced
2:00remaining
Which Compare-Object command correctly compares two custom objects by a property?
You have two arrays of objects:
$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?
ACompare-Object $a $b -Property Name
BCompare-Object $a $b -Property Age
CCompare-Object $a $b -IncludeEqual
DCompare-Object $a $b -PassThru
Attempts:
2 left
💡 Hint
Use -Property to specify which property to compare.
💻 Command Output
advanced
2:00remaining
What is the output count of Compare-Object when comparing identical arrays without -IncludeEqual?
Given:
$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
A1
B3
C0
D6
Attempts:
2 left
💡 Hint
Without -IncludeEqual, identical items are not shown.
🚀 Application
expert
3: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.
$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'
ACompare-Object $b $a | Where-Object { $_.SideIndicator -eq '=>' } | ForEach-Object { $_.InputObject }
BCompare-Object $a $b | Where-Object { $_.SideIndicator -eq '<=' } | ForEach-Object { $_.InputObject }
CCompare-Object $b $a | Where-Object { $_.SideIndicator -eq '<=' } | ForEach-Object { $_.InputObject }
DCompare-Object $a $b | Where-Object { $_.SideIndicator -eq '=>' } | ForEach-Object { $_.InputObject }
Attempts:
2 left
💡 Hint
SideIndicator '=>' means item is only in the second collection passed to Compare-Object.