0
0
PowerShellscripting~10 mins

Logical operators (-and, -or, -not) in PowerShell - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Logical operators (-and, -or, -not)
Evaluate first condition
Evaluate second condition
Apply -and / -or / -not
Result: True or False
Use result in script decision
Logical operators combine or invert true/false conditions to decide script flow.
Execution Sample
PowerShell
$a = $true
$b = $false
$result1 = $a -and $b
$result2 = $a -or $b
$result3 = -not $b
Write-Output "$result1, $result2, $result3"
This script tests -and, -or, and -not with true and false values and prints results.
Execution Table
StepExpressionEvaluationResult
1$a -and $bTrue -and FalseFalse
2$a -or $bTrue -or FalseTrue
3-not $b-not FalseTrue
4Write-OutputOutputs resultsFalse, True, True
💡 All logical operations evaluated and results output, script ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
$aTrueTrueTrueTrueTrue
$bFalseFalseFalseFalseFalse
$result1UndefinedFalseFalseFalseFalse
$result2UndefinedUndefinedTrueTrueTrue
$result3UndefinedUndefinedUndefinedTrueTrue
Key Moments - 3 Insights
Why does $a -and $b result in False even though $a is True?
Because -and requires both sides to be True. Here $b is False, so the whole expression is False (see execution_table step 1).
What does -not do to a False value?
-not reverses the value. So -not False becomes True (see execution_table step 3).
Why is $a -or $b True when $b is False?
-or needs only one True side. Since $a is True, the whole expression is True (see execution_table step 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result of $a -and $b at step 1?
ATrue
BFalse
CUndefined
DError
💡 Hint
Check the 'Result' column in execution_table row for step 1.
At which step does the script output the final results?
AStep 2
BStep 3
CStep 4
DStep 1
💡 Hint
Look for the Write-Output action in the execution_table.
If $b was True instead of False, what would $result1 be after step 1?
ATrue
BFalse
CUndefined
DError
💡 Hint
Recall that -and returns True only if both sides are True (see key_moments about -and).
Concept Snapshot
Logical operators combine true/false values:
 -and: True if both true
 -or: True if any true
 -not: reverses value
Use to control script decisions based on conditions.
Full Transcript
This lesson shows how PowerShell logical operators -and, -or, and -not work. We start with two variables: $a is True, $b is False. We test $a -and $b which is False because both must be True. Then $a -or $b is True because only one side needs to be True. Finally, -not $b reverses False to True. The script outputs these results. Understanding these helps decide what code runs based on conditions.