PowerShell Decision Making

Decision making statements come in two forms in PowerShell, ‘if’ statements and ‘switch’ statements. Both allow a decision to be made between two or more options.

‘if’ Statements

In its most basic form an ‘if’ statement executes a group of statements if an expression evaluates to true. Its basic syntax is as follows.

if (expression)
{
   # Statement(s) will execute if the expression is true.
}

The following example checks whether the values of two variables are the same and displays a message if they are.

$a = 10
$b = 10

if ($a -eq $b)
{
    Write-Host "a is equal to b"
}

This can be extended to execute a statement or statements if the expression is false as follows.

if (expression)
{
    # Statement(s) will execute if the expression is true.
}
else
{
    # Statement(s) will execute if the expression is false.
}

The ‘if’ statement can be further extended with the use of ‘elseif’. Any number of ‘elseif’ statements can be used to extend the decision making process.

if (expression)
{
    # Statement(s) will execute if the expression is true.
}
elseif (expression)
{
    # Statement(s) will execute if the first expression is false
    # and the second expression is true.
}
else
{
    # Statement(s) will execute if both the expressions are false.
}

‘if’ statements can also be nested one inside another.

if (expression)
{
    # Statement(s) will execute if the expression is true.

    if (expression)
    {
        # Statement(s) will execute if the expression is true.
    }
}

Below are some examples of ‘if’ statements using the logical operators discussed in the previous section on operators.

$a = $TRUE
$b = $TRUE
$C = $FALSE

if ($a -and $b)
{
    # Display a message if 'a' and 'b' are true.
    Write-Host "Condition 1 is true"
}
if ($c -or $b)
{
    # Display a message if 'c' or 'b' are true.
    Write-Host "Condition 2 is true"
}
if ($c -and $b)
{
    # Display a message if 'c' and 'b' are true.
    Write-Host "Condition 3 is true"
}
else
{
    # Display a message if 'c' and/or 'b' are not true.
    Write-Host "Condition 3 is not true"
}
if (!($c -and $b))
{
    # Display a message if 'c' and/or 'b' are not true.
    Write-Host "Condition 4 is true"
}

The results of running the above will be as follows.

Condition 1 is true
Condition 2 is true
Condition 3 is not true
Condition 4 is true

‘switch’ statements

Where there are more than two options in a decision, a ‘switch’ statement can be more efficient than using multiple ‘elseif’ statements.

switch (expression)
{
    expression-result-option1
    {
        # Statement(s) to execute.
        break
    }
    expression-result-option2
    {
        # Statement(s) to execute.
        break
    }
    expression-result-option3
    {
        # Statement(s) to execute.
        break
    }
    default
    {
        # statement(s) to execute.
        break
    }
}

A simple example of a ‘switch’ statement would be to display a message depending on the value of a variable.

$a = 3

switch ($a)
{
    1
    {
        Write-Host "a equals 1"
        break
    }
    2
    {
        Write-Host "a equals 2"
        break
    }
    3
    {
        Write-Host "a equals 3"
        break
    }
    default
    {
        Write-Host "a is not equal to 1, 2 or 3"
        break
    }
}

A ‘switch’ statement can have any number of options. When the ‘break’ statement is reached, the flow of control jumps to the statement following the end of the ‘switch’. The final ‘default’ statement is a catch all if none of the other options evaluate to true and is optional.