0
0
PowerShellscripting~30 mins

Compare-Object for differences in PowerShell - Mini Project: Build & Apply

Choose your learning style9 modes available
Compare-Object for differences
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use Write-Output "Joined team: $($joined -join ', ')" to print the joined names separated by commas.