Bird
Raised Fist0
PowerShellscripting~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the PowerShell cmdlet Compare-Object primarily do?
easy
A. It sorts a list of objects alphabetically.
B. It compares two sets of objects and shows their differences.
C. It merges two lists into one combined list.
D. It deletes duplicate items from a list.

Solution

  1. Step 1: Understand the purpose of Compare-Object

    Compare-Object is designed to find differences between two collections of objects.
  2. Step 2: Identify what it outputs

    It outputs which items are unique to each set, showing differences clearly.
  3. Final Answer:

    It compares two sets of objects and shows their differences. -> Option B
  4. Quick Check:

    Compare-Object = Shows differences [OK]
Hint: Compare-Object shows differences between two lists [OK]
Common Mistakes:
  • Thinking it merges lists instead of comparing
  • Confusing it with sorting or filtering commands
  • Assuming it removes duplicates
2. Which of the following is the correct syntax to compare two arrays $a and $b using Compare-Object?
easy
A. Compare-Object $a -RefObject $b
B. Compare-Object -Reference $a -Difference $b
C. Compare-Object -DifferenceObject $a -ReferenceObject $b
D. Compare-Object -ReferenceObject $a -DifferenceObject $b

Solution

  1. Step 1: Recall parameter names for Compare-Object

    The cmdlet uses -ReferenceObject for the first set and -DifferenceObject for the second set.
  2. Step 2: Match correct parameter order

    Correct syntax is Compare-Object -ReferenceObject $a -DifferenceObject $b.
  3. Final Answer:

    Compare-Object -ReferenceObject $a -DifferenceObject $b -> Option D
  4. Quick Check:

    ReferenceObject first, DifferenceObject second [OK]
Hint: Use -ReferenceObject first, then -DifferenceObject [OK]
Common Mistakes:
  • Swapping parameter names
  • Using positional parameters incorrectly
  • Omitting parameter names causing confusion
3. Given the following code:
$a = 1, 2, 3, 4
$b = 3, 4, 5, 6
Compare-Object -ReferenceObject $a -DifferenceObject $b

What is the output?
medium
A. 3 and 4 with SideIndicator '<=', 5 and 6 with SideIndicator '=>'
B. 1, 2, 3, 4, 5, 6 all with SideIndicator '=='
C. 1 and 2 with SideIndicator '<=', 5 and 6 with SideIndicator '=>'
D. No output because all items match

Solution

  1. Step 1: Identify unique items in each array

    $a has 1, 2 unique; $b has 5, 6 unique; 3 and 4 are common.
  2. Step 2: Understand SideIndicator meaning

    Items only in $a show '<='; items only in $b show '=>'.
  3. Final Answer:

    1 and 2 with SideIndicator '<=', 5 and 6 with SideIndicator '=>' -> Option C
  4. Quick Check:

    Unique items show with correct SideIndicator [OK]
Hint: SideIndicator '<=' means only in ReferenceObject [OK]
Common Mistakes:
  • Including common items in output
  • Mixing up SideIndicator symbols
  • Assuming no output if some items match
4. You run this code:
$list1 = 'apple', 'banana', 'cherry'
$list2 = 'banana', 'cherry', 'date'
Compare-Object $list1 $list2 -IncludeEqual

But you get an error. What is the likely cause?
medium
A. Missing parameter names -ReferenceObject and -DifferenceObject
B. The -IncludeEqual parameter is not valid for Compare-Object
C. The arrays contain strings, which Compare-Object cannot handle
D. The syntax is correct; error is from elsewhere

Solution

  1. Step 1: Check parameter usage

    Compare-Object requires named parameters -ReferenceObject and -DifferenceObject when using switches like -IncludeEqual.
  2. Step 2: Verify syntax validity

    Positional arguments do not bind correctly with -IncludeEqual; missing parameter names cause error.
  3. Final Answer:

    Missing parameter names -ReferenceObject and -DifferenceObject -> Option A
  4. Quick Check:

    Named parameters required with -IncludeEqual [OK]
Hint: Use named parameters with -IncludeEqual to avoid errors [OK]
Common Mistakes:
  • Thinking named parameters are not required with switches
  • Believing -IncludeEqual is invalid
  • Assuming strings cannot be compared
5. You have two lists:
$oldList = 'red', 'green', 'blue', 'yellow'
$newList = 'green', 'blue', 'purple', 'yellow'

You want to find items removed and added, but also keep items unchanged. Which command will do this?
hard
A. Compare-Object -ReferenceObject $oldList -DifferenceObject $newList -IncludeEqual
B. Compare-Object $oldList $newList -PassThru
C. Compare-Object -ReferenceObject $newList -DifferenceObject $oldList
D. Compare-Object -ReferenceObject $oldList -DifferenceObject $newList -ExcludeDifferent

Solution

  1. Step 1: Understand the goal

    You want to see added, removed, and unchanged items clearly.
  2. Step 2: Use -IncludeEqual to show unchanged items

    The -IncludeEqual option adds items that are the same in both lists to the output.
  3. Step 3: Confirm correct parameter usage

    Use -ReferenceObject for old list and -DifferenceObject for new list to see changes from old to new.
  4. Final Answer:

    Compare-Object -ReferenceObject $oldList -DifferenceObject $newList -IncludeEqual -> Option A
  5. Quick Check:

    -IncludeEqual shows unchanged items [OK]
Hint: Add -IncludeEqual to see unchanged items too [OK]
Common Mistakes:
  • Omitting -IncludeEqual and missing unchanged items
  • Swapping ReferenceObject and DifferenceObject
  • Using invalid parameters like -ExcludeDifferent
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard