Conditional Statements

Condition TypeDescriptionSyntaxExample
If StatementExecutes a block of code if a specified condition is true.if (<condition>) { <code> }powershell<br>$a = 10<br>if ($a -gt 5) {<br> Write-Output "a is greater than 5"<br>}<br>
If-Else StatementExecutes one block of code if the condition is true, and another block if it is false.if (<condition>) { <code> } else { <code> }powershell<br>$a = 3<br>if ($a -gt 5) {<br> Write-Output "a is greater than 5"<br>} else {<br> Write-Output "a is 5 or less"<br>}<br>
If-ElseIf-Else StatementAllows multiple conditions to be evaluated in sequence. The first true condition’s block is executed.if (<condition1>) { <code> } elseif (<condition2>) { <code> } else { <code> }powershell<br>$a = 7<br>if ($a -gt 10) {<br> Write-Output "a is greater than 10"<br>} elseif ($a -gt 5) {<br> Write-Output "a is greater than 5 but 10 or less"<br>} else {<br> Write-Output "a is 5 or less"<br>}<br>
Switch StatementCompares a single expression against a list of possible values and executes the matching block of code.switch (<expression>) { <match1> { <code> } <match2> { <code> } default { <code> } }powershell<br>$day = "Monday"<br>switch ($day) {<br> "Monday" { Write-Output "Start of the workweek" }<br> "Friday" { Write-Output "End of the workweek" }<br> default { Write-Output "Midweek" }<br>}<br>
Ternary Operator (Using If)Simulates a ternary operation (condition ? true-expr : false-expr). PowerShell doesn’t have a native ternary operator but you can simulate one with if/else.$result = if (<condition>) { <true-value> } else { <false-value> }powershell<br>$a = 10<br>$result = if ($a -gt 5) { "Greater" } else { "Lesser" }<br>Write-Output $result # Outputs: Greater<br>

Conditional Statements Advanced Scenarios:

Condition TypeDescriptionSyntaxExample
Logical Operators in ConditionsUsed to combine multiple conditions within an if or switch statement.if (<condition1> -and <condition2>) { <code> }powershell<br>$a = 10<br>$b = 20<br>if ($a -gt 5 -and $b -gt 15) {<br> Write-Output "Both are greater"<br>}<br>
Using -not OperatorNegates the result of a condition.if (-not <condition>) { <code> }powershell<br>$a = 5<br>if (-not ($a -gt 10)) {<br> Write-Output "a is not greater than 10"<br>}<br>
Using -or OperatorExecutes the block if either of the conditions is true.if (<condition1> -or <condition2>) { <code> }powershell<br>$a = 3<br>$b = 20<br>if ($a -gt 5 -or $b -gt 15) {<br> Write-Output "At least one is greater"<br>}<br>
Nested If StatementsPlaces one if statement inside another to handle complex conditional logic.if (<condition1>) { if (<condition2>) { <code> } }powershell<br>$a = 15<br>if ($a -gt 10) {<br> if ($a -lt 20) {<br> Write-Output "a is between 10 and 20"<br> }<br>}<br>
Using -eq, -ne, -lt, -le, -gt, -geCommon comparison operators to check equality, inequality, and greater/lesser conditions.if (<value1> -eq <value2>) { <code> }powershell<br>$a = 10<br>if ($a -eq 10) {<br> Write-Output "a equals 10"<br>}<br>
Null ChecksChecks if a variable or expression is null before performing operations.if ($variable -eq $null) { <code> }powershell<br>$a = $null<br>if ($a -eq $null) {<br> Write-Output "a is null"<br>}<br>
Conditional Operator (? :)PowerShell doesn’t have a built-in ternary operator, but similar behavior can be mimicked with if-else$result = if (<condition>) { <true-value> } else { <false-value> }powershell<br>$a = 5<br>$result = if ($a -gt 10) { "Greater" } else { "Lesser" }<br>Write-Output $result<br>
Type Comparison with -is OperatorChecks whether a value is of a specific type.if (<value> -is [type]) { <code> }powershell<br>$a = "Hello"<br>if ($a -is [string]) {<br> Write-Output "a is a string"<br>}<br>
Existence Check with Test-PathVerifies if a file or directory exists.if (Test-Path <path>) { <code> }powershell<br>$path = "C:\\example.txt"<br>if (Test-Path $path) {<br> Write-Output "File exists"<br>}<br>
Using Contains OperatorChecks if a collection contains a specific value.if (<collection> -contains <value>) { <code> }powershell<br>$array = @(1, 2, 3)<br>if ($array -contains 2) {<br> Write-Output "Array contains 2"<br>}<br>
Using In OperatorChecks if a value exists within a set of possible values.if (<value> -in <collection>) { <code> }powershell<br>$value = 5<br>if ($value -in @(1, 5, 10)) {<br> Write-Output "Value is in the list"<br>}<br>
Regular Expression Matching with -matchChecks if a string matches a regular expression pattern.if (<string> -match <regex>) { <code> }powershell<br>$string = "abc123"<br>if ($string -match '\\d{3}') {<br> Write-Output "String contains three digits"<br>}<br>