A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.
Syntax
The syntax of enhanced for loop is −
Summary of PowerShell’s If Construction. When it comes to filtering output, one of the oldest and best statements is the ‘If’ clause. As usual, the secret of understanding the syntax is to pay close attention to the style of bracket.
The following rules apply to a switch statement −
The variable used in a switch statement can only be any object or an array of objects.
You can have any number of case statements within a switch. Each case is followed by optional action to be performed.
The value for a case must be the same data type as the variable in the switch and it must be a constant or a literal.
When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.
When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.
Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.
![Powershell if statement examples Powershell if statement examples](http://powershelltutorial.net/Images/Home/Powershell-if-else-example-1.png)
Flow Diagram
![Powershell If Statement Powershell If Statement](http://media.bestofmicro.com/Y/Y/453562/gallery/Test-Path_w_600.png)
Example 1
Example of Switch statment without break statment.
This will produce the following result −
Output
Example 2
Example of Switch statment with break statment.
This will produce the following result −
Output
Example 3
Example of Switch statment with array as input.
This will produce the following result −