Decision Making - Or else...
Getting data into variables and making changes to them are helpful but being able to make decisions in your code based on those variables is where a programming language really starts to become powerful. In Powershell there are a couple of decisions making statements available to use.
If
If is used to run specific commands if a statement is evaluated to true. If the statement is true a subset of commands is run, otherwise they are skipped over. This is helpful for when you need to act in only a specific set of circumstances. An If statement is structured by a set of parentheses that follow the word if, inside the parentheses is an evaluation (generally a comparison, but there are other tests that can be uses inside an if statement). Following that is a script block (marked by opening and closing {} brackets) which contains the code that needs to run if the if statement is evaluated as true.
In the second example above $Value is evaluated twice to see if it is even or odd. Which is valid code but there is a simpler way to do it. Enter the else statement.
Else
The else statement can be paired with the if statement, so that if the if statement does not resolve, the else statement will run. We can rewrite the above example to only need one evaluation
An if statement doesn't have to have an else attached to it, but an else statement always needs to have an if before it.
If/Else if/Else
Sometimes you need to evaluate a second or third condition before you get to the catch-all of the else. To do this you can use a elseif statement. An elseif statement will only be evaluated if the if statement and any other elseif statements preceding it return false. You can have as many elseif statements as you need, however you can only ever have one if and one else per decision tree.
Switch
While you can use as many elseif statements as you need they can get cumbersome and difficult to read. In many cases instead of using an if/elseif/else tree you can use a switch. A switch takes an input (usually a variable) and then executes a code block based on a match against that variable.
#Output if it is the Weekend or a Weekday
$Day = "Friday"
switch($Day)
{
"Saturday" {Write-Host "It is the Weekend!"}
"Sunday" {Write-Host "It is the Weekend!"}
default {Write-Host "It is not the Weekend"}
}
The default match is not required but will be run if nothing else matches the item in the switch parentheses.
There is a difference in switches in Powershell and other languages. In some other languages, once a match has been made all code after that point will be ran even if it's not part of the match, meaning you need to include a break statement to stop the code after each code block. In Powershell this is not required as only the items that match will run. However, multiple matches can be made in some instances and all matching code blocks will run. In this instance, it is best to use a continue statement to keep only the code you want running.
Continue vs Break
We'll talk about both of these more in the article about Loops where they make more sense. For now, understand that Break stops every thing going on in the current code block and goes to the next piece of code and Continue stops processing in this loop and goes to the next item in line to process. In the above code, both would function the same, however there's a neat trick with a Switch and Array that you can do where it is important to use continue.
Switching on Arrays
If you use a switch on an array it will evaluate each item in that array individually. This allows you to perform a lot of the same actions on a set of data similar to a loop.