0
0
PowerShellscripting~20 mins

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

Choose your learning style9 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.