Boolean values help you decide if something is true or false. They are like simple yes/no answers in your scripts.
Boolean values in PowerShell
Introduction
Syntax
PowerShell
$variable = $true $variable = $false
Boolean values in PowerShell are $true and $false.
They are case-insensitive but usually written with a dollar sign and lowercase letters.
Examples
$isReady to true and prints it.PowerShell
$isReady = $true Write-Output $isReady
$isComplete to false and prints it.PowerShell
$isComplete = $false Write-Output $isComplete
$check.PowerShell
$check = (5 -gt 3) Write-Output $check
Sample Program
This script uses a Boolean variable $isSunny to decide what message to print.
PowerShell
$isSunny = $true if ($isSunny) { Write-Output "It is sunny outside." } else { Write-Output "It is not sunny outside." }
Important Notes
Boolean values are very useful for controlling the flow of your script with if statements.
Remember, anything that is not explicitly $false or $null is treated as true in conditions.
Summary
Boolean values are $true or $false in PowerShell.
They help your script make decisions.
Use them with if statements to run code only when conditions are met.
Practice
1. What are the two Boolean values used in PowerShell?
easy
Solution
Step 1: Recall PowerShell Boolean syntax
PowerShell uses special keywords starting with $ for Boolean values.Step 2: Identify correct Boolean values
The correct Boolean values are $true and $false, not plain True/False or numbers.Final Answer:
$true and $false -> Option AQuick Check:
Boolean values = $true, $false [OK]
Hint: Remember PowerShell Booleans start with $ [OK]
Common Mistakes:
- Using True/False without $
- Using 1 and 0 as Booleans
- Confusing Yes/No as Boolean
2. Which of the following is the correct way to assign a Boolean value in PowerShell?
easy
Solution
Step 1: Check assignment syntax in PowerShell
PowerShell uses = to assign values to variables, and variables start with $.Step 2: Identify correct Boolean value and syntax
The Boolean value must be $true with $ sign, and assignment uses =.Final Answer:
$flag = $true -> Option AQuick Check:
Assign Boolean with $true and = [OK]
Hint: Use $true with = for Boolean assignment [OK]
Common Mistakes:
- Missing $ on Boolean value
- Using := instead of =
- Missing $ on variable name
3. What will this PowerShell code output?
if ($false) { "Yes" } else { "No" }medium
Solution
Step 1: Understand the if condition
The condition is $false, which means the if block will NOT run.Step 2: Determine which block runs
Since the if condition is false, the else block runs and outputs "No".Final Answer:
No -> Option CQuick Check:
if $false runs else block = No [OK]
Hint: If condition false runs else block [OK]
Common Mistakes:
- Thinking $false runs if block
- Expecting output $false instead of string
- Confusing syntax causing error
4. Identify the error in this PowerShell snippet:
if ($True) { Write-Output "Yes" } else { Write-Output "No" }medium
Solution
Step 1: Check Boolean case sensitivity
PowerShell Boolean values are case-insensitive, so $True works like $true.Step 2: Verify syntax correctness
The if statement syntax is correct with parentheses and Write-Output is valid.Final Answer:
No error, code runs fine -> Option DQuick Check:
PowerShell Booleans are case-insensitive [OK]
Hint: PowerShell Booleans ignore case [OK]
Common Mistakes:
- Thinking $True is invalid case
- Expecting syntax error without parentheses
- Believing Write-Output can't be used here
5. You want to run a script block only if a variable $isReady is true. Which is the best way to check this in PowerShell?
hard
Solution
Step 1: Understand Boolean checks in PowerShell
PowerShell treats variables as Boolean in if conditions directly, so if ($isReady) works.Step 2: Analyze other comparisons
Checking explicitly with -eq $true or -ne $false also works to test if $isReady is true.Step 3: Confirm all options are valid
All three ways correctly check if $isReady is true and run the block.Final Answer:
All of the above -> Option BQuick Check:
Multiple ways to test Boolean true in PowerShell [OK]
Hint: You can test Booleans directly or with -eq/-ne [OK]
Common Mistakes:
- Thinking only direct if ($isReady) works
- Confusing -eq $true with assignment
- Ignoring that -ne $false also works
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
