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.

Wolfgang Sommergut

Wolfgang Sommergut has over 20 years of experience in IT journalism. He has also worked as a system administrator and as a tech consultant. Today he runs the German publication WindowsPro.de.

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.

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:

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:

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.

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:

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:

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.

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.

Join the 4sysops PowerShell group!

Your question was not answered? Ask in the forum!

1+
Share
2 Comments
  1. Mike 3 years ago

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

    2+

  2. Kevin Marquette 2 years ago

    I just wrote an in-depth breakdown of the switch statement in PowerShell that your readers may appreciate: https://kevinmarquette.github.io/2018-01-12-Powershell-switch-statement

    1+

Leave a reply

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

*

© 4sysops 2006 - 2019

CONTACT US

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

Sending

Log in with your credentials

or    

Forgot your details?

Create Account