Bird
Raised Fist0
PowerShellscripting~20 mins

Logical operators (-and, -or, -not) in PowerShell - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Logical Operator Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this PowerShell command?
Consider the following command:
Write-Output ($true -and $false) -or $true

What will this output?
PowerShell
Write-Output ($true -and $false) -or $true
AFalseTrue
BFalse
CTrueFalse
DTrue
Attempts:
2 left
💡 Hint
Remember that -and has higher precedence than -or in PowerShell.
💻 Command Output
intermediate
2:00remaining
What does this PowerShell expression output?
Evaluate this expression:
Write-Output -not ($false -or $false)

What is the output?
PowerShell
Write-Output -not ($false -or $false)
AFalse
BTrue
CFalseTrue
DTrueFalse
Attempts:
2 left
💡 Hint
Check what ($false -or $false) evaluates to before applying -not.
📝 Syntax
advanced
2:00remaining
Which option causes a syntax error in PowerShell?
Identify the option that will cause a syntax error when run in PowerShell:
AWrite-Output ($true -and not $false)
BWrite-Output ($true -or -not $false)
CWrite-Output (-not ($true -or $false))
DWrite-Output ($true -and -not $false)
Attempts:
2 left
💡 Hint
Check the correct usage of the -not operator in PowerShell.
🔧 Debug
advanced
2:00remaining
Why does this PowerShell script output 'True' instead of 'False'?
Given this script:
$a = $true
$b = $false
if ($a -or $b -and $false) { Write-Output 'True' } else { Write-Output 'False' }

Why does it output 'True' instead of 'False'?
PowerShell
$a = $true
$b = $false
if ($a -or $b -and $false) { Write-Output 'True' } else { Write-Output 'False' }
ABecause -and has higher precedence than -or, so $b -and $false is evaluated first, resulting in False, then $a -or False is True, so output should be True.
BBecause the entire condition is evaluated as ($a -or $b) -and $false, which is False.
CBecause the script has a syntax error and defaults to output 'False'.
DBecause -or has higher precedence than -and, so $a -or $b is True, then True -and $false is False, so output is False.
Attempts:
2 left
💡 Hint
Check operator precedence carefully in PowerShell.
🚀 Application
expert
3:00remaining
Which script correctly filters files modified in the last 7 days and larger than 1MB?
You want to list files in 'C:\Logs' that were modified in the last 7 days AND are larger than 1MB. Which script correctly uses logical operators to do this?
AGet-ChildItem C:\Logs | Where-Object { -not ($_.LastWriteTime -lt (Get-Date).AddDays(-7)) -and $_.Length -gt 1MB }
BGet-ChildItem C:\Logs | Where-Object { $_.LastWriteTime -ge (Get-Date).AddDays(-7) -and-or $_.Length -gt 1MB }
CGet-ChildItem C:\Logs | Where-Object { $_.LastWriteTime -ge (Get-Date).AddDays(-7) -and $_.Length -gt 1MB }
DGet-ChildItem C:\Logs | Where-Object { $_.LastWriteTime -ge (Get-Date).AddDays(-7) -or $_.Length -gt 1MB }
Attempts:
2 left
💡 Hint
Check the logical operators and syntax carefully.

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

  1. Step 1: Understand the meaning of -and

    The -and operator requires both conditions to be true for the entire expression to be true.
  2. Step 2: Compare with other operators

    -or means one or both conditions true; -not reverses a condition; -xor means exactly one true.
  3. Final Answer:

    -and -> Option C
  4. 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' }

Solution

  1. Step 1: Identify PowerShell logical operator syntax

    PowerShell uses -and, -or, -not for logical operations, not && or > for comparisons.
  2. Step 2: Check comparison operators and logical operator

    -gt and -lt are correct for greater than and less than; -and is correct for AND.
  3. Final Answer:

    if ($a -gt 5 -and $b -lt 10) { Write-Output 'true' } -> Option B
  4. Quick 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
A. Yes
B. No
C. false
D. true

Solution

  1. Step 1: Evaluate $a -or $b

    $a is true, $b is false; -or means if either is true, result is true.
  2. Step 2: Determine if block output

    Since $a -or $b is true, the if block runs and outputs 'Yes'.
  3. Final Answer:

    Yes -> Option A
  4. Quick 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
A. No error, statement is correct
B. Incorrect use of -and operator
C. Variables $a and $b are undefined
D. Missing parentheses around -not condition

Solution

  1. Step 1: Understand operator precedence

    -not has higher precedence; without parentheses, it applies only to $a, but combined with -and can cause confusion.
  2. Step 2: Fix with parentheses

    Use parentheses: if ((-not $a) -and $b) { ... } to clarify logic and avoid errors.
  3. Final Answer:

    Missing parentheses around -not condition -> Option D
  4. Quick 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
A. if (($x -ne 10) -and ($y -eq 5 -or $y -eq 7)) { 'Match' }
B. if (-not ($x -eq 10 -and $y -eq 5 -or $y -eq 7)) { 'Match' }
C. if (-not $x -eq 10 -and $y -eq 5 -or $y -eq 7) { 'Match' }
D. if ($x -ne 10 -or $y -eq 5 -and $y -eq 7) { 'Match' }

Solution

  1. 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).
  2. Step 2: Combine with -and and parentheses

    Use parentheses to group $y conditions and combine with $x condition using -and.
  3. Final Answer:

    if (($x -ne 10) -and ($y -eq 5 -or $y -eq 7)) { 'Match' } -> Option A
  4. Quick 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 strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard