📖 Scenario: You are managing a list of employees and their work statuses. You want to check who is eligible for a bonus based on their attendance and performance.
🎯 Goal: Build a PowerShell script that uses logical operators -and and -not to filter employees based on their attendance and performance.
📋 What You'll Learn
Create a dictionary called employees with employee names as keys and a nested dictionary as values containing attendance and performance scores.
Create a variable called attendance_threshold set to 80.
Use a foreach loop with variables name and info to iterate over employees.GetEnumerator().
Inside the loop, use logical operators -and and -not to check if an employee's attendance is above the threshold and performance is good.
Print the names of employees who qualify for the bonus.
💡 Why This Matters
🌍 Real World
Filtering employee data based on multiple conditions is common in HR and management tasks to decide bonuses or promotions.
💼 Career
Understanding logical operators and loops in PowerShell helps automate administrative tasks and data filtering in IT and business roles.
Progress0 / 4 steps
1
Create the employee data
Create a dictionary called employees with these exact entries: 'Alice' with attendance 90 and performance 85, 'Bob' with attendance 75 and performance 95, 'Charlie' with attendance 82 and performance 70.
PowerShell
Hint
Use @{} to create nested dictionaries for each employee.
2
Set the attendance threshold
Create a variable called attendance_threshold and set it to 80.
PowerShell
Hint
Just assign the number 80 to $attendance_threshold.
3
Use logical operators to find eligible employees
Use a foreach loop with variables name and info to iterate over $employees.GetEnumerator(). Inside the loop, use the logical operator -and to check if info.attendance is greater than or equal to $attendance_threshold and info.performance is greater than 80. Also, use -not to exclude employees with performance less than 75. Store the names of employees who meet these conditions in a list called eligible.
PowerShell
Hint
Use foreach ($entry in $employees.GetEnumerator()) and inside the loop check conditions with -and and -not. Add qualifying names to $eligible.
4
Print the eligible employees
Write a Write-Output statement to print the list $eligible.
PowerShell
Hint
Use Write-Output $eligible to show the names of employees who qualify.
Practice
(1/5)
1. Which logical operator in PowerShell means both conditions must be true for the whole expression to be true?
easy
A. -xor
B. -or
C. -and
D. -not
Solution
Step 1: Understand the meaning of -and
The -and operator requires both conditions to be true for the entire expression to be true.
Step 2: Compare with other operators
-or means one or both conditions true; -not reverses a condition; -xor means exactly one true.
Final Answer:
-and -> Option C
Quick Check:
-and = both true [OK]
Hint: Remember: -and means both must be true [OK]
Common Mistakes:
Confusing -and with -or
Thinking -not means both true
Mixing -and with -xor
2. Which of the following is the correct syntax to check if variable $a is greater than 5 AND $b is less than 10 in PowerShell?
easy
A. if ($a -gt 5 && $b -lt 10) { Write-Output 'true' }
B. if ($a -gt 5 -and $b -lt 10) { Write-Output 'true' }
C. if ($a > 5 && $b < 10) { Write-Output 'true' }
D. if ($a > 5 -and $b < 10) { Write-Output 'true' }