0
0
PowerShellscripting~5 mins

If-elseif-else statements in PowerShell - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of an if statement in PowerShell?
An if statement checks a condition and runs code only if that condition is true. It helps make decisions in scripts.
Click to reveal answer
beginner
How do you write an elseif statement in PowerShell?
Use the keyword 'elseif' followed by a condition in parentheses, then a script block with the code to run if that condition is true.
Click to reveal answer
beginner
What does the else block do in an if-elseif-else structure?
The else block runs code when none of the previous if or elseif conditions are true. It acts as a fallback.
Click to reveal answer
beginner
Show the basic syntax of an if-elseif-else statement in PowerShell.
if (<condition1>) { # code if condition1 is true } elseif (<condition2>) { # code if condition2 is true } else { # code if none are true }
Click to reveal answer
intermediate
Why is it useful to use elseif instead of multiple if statements?
Using elseif checks conditions one by one and stops when one is true, making scripts faster and easier to read compared to many separate ifs.
Click to reveal answer
What keyword starts an alternative condition after an if in PowerShell?
Aelif
Belse if
Celseif
Dotherwise
What happens if none of the if or elseif conditions are true and there is an else block?
ANothing happens
BThe else block runs
CAn error occurs
DThe script stops
Which symbol is used to start a condition in PowerShell if statements?
A() parentheses
B[] brackets
C{} braces
D<> angle brackets
Can you have multiple elseif blocks in one if statement?
ANo, elseif is not valid in PowerShell
BNo, only one elseif is allowed
CYes, but only two
DYes, you can have many elseif blocks
What is the correct way to write an else block?
Aelse { # code }
Belse () { # code }
Celse if { # code }
Delseif { # code }
Explain how if-elseif-else statements control the flow of a PowerShell script.
Think about how the script chooses which code to run based on conditions.
You got /5 concepts.
    Write a simple PowerShell if-elseif-else script that checks a number and prints if it is positive, negative, or zero.
    Use comparison operators like -gt and -lt for greater than and less than.
    You got /4 concepts.