📖 Scenario: You work in a small office where you keep two lists of employee names. One list is the current team, and the other is the team from last year. You want to find out who joined or left the team.
🎯 Goal: Build a PowerShell script that uses Compare-Object to find differences between two lists of employee names.
📋 What You'll Learn
Create two arrays called currentTeam and lastYearTeam with exact names
Create a variable called differences to store the comparison result using Compare-Object
Use Compare-Object with the -PassThru parameter to get the differing names
Print the differences variable to show who joined or left
💡 Why This Matters
🌍 Real World
Comparing lists of employees, files, or settings to find changes is common in IT and office work.
💼 Career
Knowing how to use Compare-Object helps in system administration, auditing, and automation tasks.
Progress0 / 4 steps
1
Create the two team lists
Create two arrays called currentTeam and lastYearTeam with these exact names: currentTeam: "Alice", "Bob", "Charlie", "Diana" lastYearTeam: "Bob", "Charlie", "Eve", "Frank"
PowerShell
Hint
Use @("name1", "name2", ...) to create arrays in PowerShell.
2
Compare the two lists
Create a variable called differences that stores the result of Compare-Object comparing $currentTeam and $lastYearTeam with the -PassThru parameter.
PowerShell
Hint
Use Compare-Object -ReferenceObject $lastYearTeam -DifferenceObject $currentTeam -PassThru to get the differing names.
3
Filter differences by side indicator
Use a foreach loop with variables name and side to iterate over Compare-Object output (without -PassThru) comparing $lastYearTeam and $currentTeam. Create two arrays called joined and left to store names who joined (side is '=>') and left (side is '<=') respectively.
PowerShell
Hint
Use $diff.InputObject and $diff.SideIndicator inside the loop to check each difference.
4
Print who joined and who left
Print the arrays joined and left using Write-Output with these exact messages: "Joined team: " followed by the joined names separated by commas, and "Left team: " followed by the left names separated by commas.
PowerShell
Hint
Use Write-Output "Joined team: $($joined -join ', ')" to print the joined names separated by commas.
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
Step 1: Understand the purpose of Compare-Object
Compare-Object is designed to find differences between two collections of objects.
Step 2: Identify what it outputs
It outputs which items are unique to each set, showing differences clearly.
Final Answer:
It compares two sets of objects and shows their differences. -> Option B
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
Step 1: Recall parameter names for Compare-Object
The cmdlet uses -ReferenceObject for the first set and -DifferenceObject for the second set.
Step 2: Match correct parameter order
Correct syntax is Compare-Object -ReferenceObject $a -DifferenceObject $b.
Final Answer:
Compare-Object -ReferenceObject $a -DifferenceObject $b -> Option D
Quick Check:
ReferenceObject first, DifferenceObject second [OK]
Hint: Use -ReferenceObject first, then -DifferenceObject [OK]