📖 Scenario: You are creating a simple script to check if a user is allowed to access a system based on their status.
🎯 Goal: Build a PowerShell script that uses Boolean values to represent user access status and prints the result.
📋 What You'll Learn
Create a Boolean variable named isUserActive with the value $true.
Create a Boolean variable named hasAccessRights with the value $false.
Use a Boolean expression to check if the user is active and has access rights.
Print the result of the Boolean expression.
💡 Why This Matters
🌍 Real World
Boolean values are used in scripts to represent true/false conditions like user status, feature flags, or system states.
💼 Career
Understanding Boolean logic is essential for automation tasks, decision-making in scripts, and controlling program flow in IT and DevOps roles.
Progress0 / 4 steps
1
Create Boolean variables
Create a Boolean variable called isUserActive and set it to $true. Also create a Boolean variable called hasAccessRights and set it to $false.
PowerShell
Hint
Use $true and $false to assign Boolean values in PowerShell.
2
Create access check variable
Create a Boolean variable called canAccess that is $true only if both isUserActive and hasAccessRights are $true. Use the -and operator.
PowerShell
Hint
Use -and to combine two Boolean variables in PowerShell.
3
Use if statement to check access
Write an if statement that checks if canAccess is $true. Inside the if, set a variable message to 'Access granted'. Otherwise, set message to 'Access denied'.
PowerShell
Hint
Use if (condition) { } else { } to choose between two messages.
4
Print the access message
Print the value of the variable message using Write-Output.
PowerShell
Hint
Use Write-Output $message to print the message to the console.
Practice
(1/5)
1. What are the two Boolean values used in PowerShell?
easy
A. $true and $false
B. True and False (without $)
C. 1 and 0
D. Yes and No
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 A
Quick 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
A. $flag = $true
B. $flag = True
C. $flag := $true
D. flag = $true
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 A
Quick 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
A. $false
B. Yes
C. No
D. Error
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".