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.

If a number of options within a 'switch' statement have the same result, it is possible to group them together.

$a = 3

switch ($a)
{
    {($_ -eq 1) -or ($_ -eq 2) -or ($_ -eq 3)}
    {
        Write-Host "a is between 1 and 3"
        break
    }
    {($_ -eq 4) -or ($_ -eq 5) -or ($_ -eq 6)}
    {
        Write-Host "a is between 4 and 6"
        break
    }
    default
    {
        Write-Host "a is not between 1 and 6"
        break
    }
}

Here, if the value of 'a' is either one, two or three, then the message, 'a is between 1 and 3' is displayed. If the value of 'a' is either four, five or six, then the message, 'a is between 4 and 6' is displayed. Finally, if the value of 'a' is not between one and six, then the 'default' catch all displays the message, 'a is not between 1 and 6'.