PowerShell offers commands or cmdlets for regulating the execution flow of your scripts, just like many other languages, and the “switch” statement is one of them. Today, we’ll cover the topic on how to use switch statements in PowerShell.
What is the Switch Statement in PowerShell?
In PowerShell, the switch command or statement is used to handle numerous “if” statements, or in other words, we can say that it replaces several “if-else” statements. If-else statements can be used to check a single condition in a script or function, but switch statements are utilized for evaluating many “if” statements.
In comparison to several “if” statements, switch statements are easier to implement for coding. Each statement has some condition, and if any of them is “true,” then some operation will be performed, which is given in the block of switch cases. Here is the flowchart of the switch statement:
Syntax of the Switch Statement
The “Switch” is the keyword used for adding a switch statement in your script. The “Value” is the parameter utilized for defining the “expression” of any “Value” which will be used in testing conditions. In curly braces { }, define your “Conditions” separately and within those curly braces { } of conditions specify the operations which are going to be performed in case if the condition is tested “true”.
{
<Condition1> {Operation 1}
<Condition2> {Operation 2}
}
Using Switch Statement in PowerShell
To demonstrate how to use the switch command in PowerShell, we will create a PowerShell script. Open your Windows PowerShell ISE and create a new file.
In this script, switch statement will take “3” as condition and will print out condition body if any of the cases matches with the “3”.
1 {"January"}
2 {"February"}
3 {"March"}
}
Save the script as “testfile1.ps1” and execute it.
Switch Statement for Matching Numeric Values in PowerShell
Utilize a switch statement to check various conditions. The switch case is similar to a series of “if” statements, but it offers an easy method for its implementation. Each condition and its operation are added in the Switch case. If a condition is true, the specified operation will execute. The automatic variables $ and $switch can be used in the switch statement.
switch ( $day )
{
0 { $result = 'Sunday' }
1 { $result = 'Monday' }
2 { $result = 'Tuesday' }
3 { $result = 'Wednesday' }
4 { $result = 'Thursday' }
5 { $result = 'Friday' }
6 { $result = 'Saturday' }
}
$result
In the below-given example, the value of $day matches one of the numeric values specified in the switch case, then the weekday is added to the $result variable. In this example, we will show you a variable assignment, but any operation can be executed in these blocks of scripts.
Execute this “testfile1.ps1” script, and it will show you the following output:
Switch Statement for Matching Strings in PowerShell
In the previous example, we were matching the numeric value. In the same manner, you can write out a script to match string values using the switch statement.
switch ( $item )
{
Component
{
'is a component'
}
Role
{
'is a role'
}
Location
{
'is a location'
}
}
Switch statement with Wildcards in PowerShell
Wildcards specify that the condition is a string which we will use as a wildcard. If the matched condition is not a string in the switch case, the Wildcard option will be ignored. You can utilize wildcards in your script by using the “-Wildcard” option. “-like” and “-Wildcard” options work on the same logic.
switch -Wildcard ( $message )
{
'Error*'
{
Write-Error -Message $Message
}
'Warning*'
{
Write-Warning -Message $Message
}
default
{
Write-Information $message
}
}
Switch statement with Regex in PowerShell
Regular Expression is another name for Regex. Switch statement checks the value of the condition against the Regular Expression or the “-Regex”. If the matched condition is not a string, the switch statement also ignores the regex option.
{
'^Error'
{
Write-Error -Message $Message
}
'^Warning'
{
Write-Warning -Message $Message
}
default
{
Write-Information $message
}
}
Execute the script, and it will give you the following output:
Switch Statement for Multiple Conditions Matching in PowerShell
The switch statement can be adapted to a variety of situations. The same condition can be added many times, and all of them will be executed if each statement is true.
{
'apple' { 'lower case apple match' }
'APPLE' { 'upper case apple match' }
'Apple' { 'mixed case apple match' }
}
Switch Statement with “continue” in PowerShell:
As the “for loop” works, “continue” moves on to the following condition or leaves the switch case if no more conditions are available. The previous example can be rewritten using “continue” statements so that only one statement is executed.
{
'apple'
{
'lower case apple is matched'
continue
}
'Apple'
{
'mixed case apple is matched'
continue
}
'APPLE'
{
'upper case word is matched'
continue
}
}
Switch Statement with “break” in PowerShell:
The switch is exited with a “break” statement. For single values, it works the same as “continue“. When processing an array, the difference is visible. Break interrupts the switch’s processing and allows it to move on to the item.
'An update is downloading'
'There are errors in the downloaded file'
'System is sending email:'
'Error: out of disk space'
'...'
)
switch -Wildcard ($Messages)
{
'Error*'
{
Write-Error -Message $PSItem
break
}
'*Error*'
{
Write-Warning -Message $PSItem
continue
}
'*Warning*'
{
Write-Warning -Message $PSItem
continue
}
default
{
Write-Output $PSItem
}
}
Execution of the above script will show you the following output:
Conclusion
Switch statement is efficient compared to using numerous “if” conditions because it reduces execution time. Complicated tasks such as evaluation of wildcards and Regex can be performed with the switch statement. This article shows you the method of using switch statements for matching numeric and string values, multiple conditions matching in PowerShell. Moreover, the use of the “wildcard”, “regex”, “continue,” and “break” options are also demonstrated.