Logical operators help you combine or change true/false conditions in scripts. They let you make decisions based on multiple rules.
Logical operators (-and, -or, -not) in PowerShell
Introduction
Syntax
PowerShell
condition1 -and condition2 condition1 -or condition2 -not condition
-and means both conditions must be true.
-or means at least one condition must be true.
-not reverses the condition's result.
Examples
PowerShell
$a = 5 $b = 10 if ($a -lt 10 -and $b -gt 5) { "Both true" }
PowerShell
$a = 3 $b = 2 if ($a -eq 3 -or $b -eq 5) { "At least one true" }
PowerShell
$flag = $false if (-not $flag) { "Flag is false" }
Sample Program
This script checks if a person is 18 or older and has an ID. If both are true, it allows entry. If no ID, it says ID required. Otherwise, it denies entry.
PowerShell
$age = 20 $hasID = $true if ($age -ge 18 -and $hasID) { Write-Output "Allowed entry" } elseif (-not $hasID) { Write-Output "ID required" } else { Write-Output "Not allowed" }
Important Notes
Use parentheses to group conditions for clarity, like: if (($a -lt 10) -and ($b -gt 5))
Logical operators work with boolean values: $true or $false.
PowerShell evaluates conditions left to right and stops early if possible (short-circuit).
Summary
-and means both conditions must be true.
-or means at least one condition must be true.
-not reverses a condition's true/false value.
Practice
1. Which logical operator in PowerShell means both conditions must be true for the whole expression to be true?
easy
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 CQuick 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
Solution
Step 1: Identify PowerShell logical operator syntax
PowerShell uses -and, -or, -not for logical operations, not && or > for comparisons.Step 2: Check comparison operators and logical operator
-gt and -lt are correct for greater than and less than; -and is correct for AND.Final Answer:
if ($a -gt 5 -and $b -lt 10) { Write-Output 'true' } -> Option BQuick Check:
PowerShell uses -and, -gt, -lt [OK]
Hint: Use -and, -gt, -lt for PowerShell conditions [OK]
Common Mistakes:
- Using && instead of -and
- Using > or < instead of -gt or -lt
- Mixing syntax from other languages
3. What is the output of this PowerShell code?
$a = $true
$b = $false
if ($a -or $b) { 'Yes' } else { 'No' }
medium
Solution
Step 1: Evaluate $a -or $b
$a is true, $b is false; -or means if either is true, result is true.Step 2: Determine if block output
Since $a -or $b is true, the if block runs and outputs 'Yes'.Final Answer:
Yes -> Option AQuick Check:
-or with true and false = true [OK]
Hint: -or is true if any condition is true [OK]
Common Mistakes:
- Thinking both must be true for -or
- Confusing output with boolean true/false
- Ignoring the if-else structure
4. Identify the error in this PowerShell statement:
if (-not $a -and $b) { Write-Output 'Valid' }medium
Solution
Step 1: Understand operator precedence
-not has higher precedence; without parentheses, it applies only to $a, but combined with -and can cause confusion.Step 2: Fix with parentheses
Use parentheses: if ((-not $a) -and $b) { ... } to clarify logic and avoid errors.Final Answer:
Missing parentheses around -not condition -> Option DQuick Check:
Use parentheses with -not in complex expressions [OK]
Hint: Use parentheses to group -not conditions [OK]
Common Mistakes:
- Ignoring operator precedence
- Assuming -not applies to whole expression
- Not using parentheses for clarity
5. You want to write a PowerShell condition that is true only when $x is NOT equal to 10 AND $y is either 5 or 7. Which is the correct expression?
hard
Solution
Step 1: Break down the condition
$x must NOT be 10, so $x -ne 10; $y must be 5 OR 7, so ($y -eq 5 -or $y -eq 7).Step 2: Combine with -and and parentheses
Use parentheses to group $y conditions and combine with $x condition using -and.Final Answer:
if (($x -ne 10) -and ($y -eq 5 -or $y -eq 7)) { 'Match' } -> Option AQuick Check:
Use parentheses to group OR inside AND [OK]
Hint: Group OR conditions inside parentheses when combined with AND [OK]
Common Mistakes:
- Missing parentheses causing wrong logic
- Using -or instead of -and between main conditions
- Incorrect placement of -not operator
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
