All procedural programming languages require constructs that allow them to execute instructions that are dependent on certain conditions. Like most programming languages, PowerShell uses the keywords if/else and switch for this purpose. However, compared to other scripting languages, PowerShell’s conditional statements come with a few syntactical peculiarities and a more powerful version of switch.

Conditional statements belong to the basic programming tasks. However, if you often switch between different scripting languages, such as PHP, JavaScript, and PowerShell, you can easily get confused about the different syntax of their conditional statements.

if statement in PowerShell ^

The following rules apply for if statements in PowerShell:

  • PowerShell commands are case-insensitive, so you can use If, if, or IF. The same applies to else.
  • The boolean condition has to be in parentheses.
  • The depending statement must be in braces.

In the simplest situation, the condition only contains one variable that has to be tested whether it is defined or unequal to zero. If this is the case, the test of the boolean condition evaluates in TRUE; otherwise, it is FALSE.

if($test) {echo "Value of test: " $test}

Conditional statements and comparison operators

Usually, you will require more complex conditions for which you need comparison operators. However, if you just want to negate one boolean variable, you can use the unary –NOT operator:

if(-NOT $test) {echo "Value of test is zero or undefined."}

Rules for conditional statements ^

Common syntactical rules apply for the statements that depend on the condition. In contrast to JavaScript or PHP, if each statement is on a separate line, you don’t have to end the line with a semicolon. However, in an interactive shell, if you want to pack multiple statements on a single line, you have to separate them with semicolons:

if(Test-Path *.gif){gci *.gif|foreach{$len += $_.length}; Write-Host $len " Bytes"}

In distinction from VBScript, you don’t need an End If in PowerShell. This is also true if an else branch follows the if statement. The else branch has the same syntax except that it doesn’t require a condition. After else follows the block that will be executed if the if condition doesn’t apply:

if($test) {echo "Value of test: " $test}
else {echo "Value of test is zero or undefined"}

Multiple conditions with elseif ^

In those cases where an if statement plus else branch is insufficient, you can combine multiple conditions. PowerShell offers the elseif keyword for this purpose.

If the evaluation of the if condition results in FALSE, all elseif conditions will be tested afterwards. If one of the elseif conditions evaluates in TRUE, the depending block will be executed, and PowerShell will exit the entire if-else-elseif block. If neither the if condition nor one of the elseif condition is true, the optional closing else condition will be tested.

if (condition 1) {command}
elseif (condition 2) {command}
elseif (condition 3) {command}
else {command}

switch as alternative to long elseif lists ^

Instead of using long if-elseif chains, it is more elegant to switch to switch (sorry for the pun). switch follows the same pattern as all languages that were syntactically derived from C, such as PHP or JavaScript. However, in PowerShell, switch is used for testing equality only. Thus, you can’t use comparison operators in switch statements.

The value to be tested, the so-called control expression or control variable, must be enclosed in parentheses right after the switch keyword, but the value (also called case) will only be compared with the control variable in the following block:

switch(Read-Host "Select a menu item"){
    1 {"File will be deleted"}
    2 {"File will be displayed"}
    3 {"File is write protected"}
    default {"Invalid entry"}

If you enter 1, 2, or 3, the corresponding message will be displayed; otherwise, you’ll see “Invalid entry.”

PowerShell tests all switch cases ^

If a match occurs, the corresponding instruction will be executed; however, PowerShell will still run through the remaining cases. If another value matches, the corresponding instruction will be executed as well.

If you want to avoid this behavior, you have to end the instruction with break. PowerShell will then exit the block whenever a value matches.

In the above example, this would look like this:

1 {"File will be deleted"; break}

Capitalization, wildcards, regex ^

By default, the comparison of strings is case-insensitive. If you don’t want this, you can add the operator -CaseSensitive to switch.

A specific feature of PowerShell is that you can use wildcards and regular expressions if you compare strings. For this purpose, you have to add the operators -wildcard and -regex.

Subscribe to 4sysops newsletter!

switch -wildcard("PowerShell"){
    "Power*" {echo "'*' stands for 'shell'"}
    *ersh*" {echo "'*' replaces 'Pow' and 'ell'"}
    "PowerShe??" {echo "Pattern matches because ?? replaces two 'l' "}

The wildcard “*” stands for multiple arbitrary characters, whereas “?” is just for one character. You can work with regular expressions in a similar way as with wildcards; you just have to add the operator -regex.

  1. Mike 5 years ago

    Thanks.  This is the clearest example of how to use switch I've come across.

  2. Kevin Marquette 4 years ago

    I just wrote an in-depth breakdown of the switch statement in PowerShell that your readers may appreciate:

Leave a reply

Please enclose code in pre tags

Your email address will not be published. Required fields are marked *


© 4sysops 2006 - 2022


Please ask IT administration questions in the forums. Any other messages are welcome.


Log in with your credentials


Forgot your details?

Create Account