Powershell - Switching off If-Else
Introduction
If/Else statements are a common way to control the flow of a program. However, in PowerShell, there is a more efficient way to do this using the Switch statement. The Switch statement is similar to the If/Else statement, but it is more concise and easier to read. In this article, we will explore how to use the Switch statement in PowerShell.
Syntax
The basic syntax of the Switch statement in PowerShell is as follows:
Switch ($Array/$Hash){
"Value1" { <# Code to execute if the value is Value1 #> }
"Value2" { <# Code to execute if the value is Value2 #> }
"Value3" { <# Code to execute if the value is Value3 #> }
Default { <# Code to execute if none of the values match #> }
}
Any unquoted value that is not recognized as a number is treated as a string. To avoid confusion or unintended string conversion, you should always quote string values. Enclose any expressions in parentheses (), creating subexpressions, to ensure that the expression is evaluated correctly.
In the case that resulting switch values are not string, you can use the typeclass [string] before the variable or use the .ToString() method after the variable to convert the object to a string. For more complex switch commands, instead of a string or numerical value, use a code block {} that returns a value of true or false. In this example, the code block on the left uses a comparison operator to validate the value of the pipeline variable "$_" passed to the switch. A true or false value is returned, and the code block on the right is executed if the value is true.
Switch ($Array/$Hash){
{ $_ -eq "Value1" } { <# Code to execute if the value is Value1 #> }
{ $_ -eq "Value2" } { <# Code to execute if the value is Value2 #> }
{ $_ -eq "Value3" } { <# Code to execute if the value is Value3 #> }
Default { <# Code to execute if none of the values match #> }
}
Example - action based on one value
Let's try an example of a switch, using a common command like get-service. We will use the switch statement to check the status of a service and perform different actions based on the status.
# Get the service information for the Windows Update service
$service = Get-Service -Name "wuauserv"
# Now use switch to check the status of the service, and return different messages based on the status.
Switch ($service.Status){
"Running" { Write-Output "The service is running." }
"Stopped" { Write-Output "The service is stopped." }
Default { Write-Output "The service is in an unknown state." }
}
In this example, we save the service information for Windows Automatic Update service in the variable $service. We then use the Switch statement to check the status of the service. If the service is running, we output a message saying "The service is running." If the service is stopped, we output a message saying "The service is stopped." If the service is in any other state, we output a message saying "The service is in an unknown state."
Example - action based on multiple values
In the previous example, we only checked the status of one service. Let's try an example where we check the status of multiple services and perform different actions based on the status of each service.
String Conversions
Switch commands default to reading number or string values, but in many cases the output of powershell commands is not a string. Here are some easy ways to convert the output of a command to a string by combining the properties into a single new string by using double quotes and enclosing the properties in $().
# Get the service information for the Windows Update service and the Print Spooler service
$service1 = Get-Service -Name "wuauserv"
$service2 = Get-Service -Name "spooler"
#Because the Status property is not a string, combine the properties into a single new string by using double quotes and enclosing the properties in $().
$ServiceString = "$($service1.Status) $($service2.Status)"
# Now use this new string in the switch statement to check the status of the services, and return different messages based on the status.
Switch ($ServiceString) {
"Running Running" { Write-Output "Both services are running." }
"Running Stopped" { Write-Output "The Windows Update service is running, but the Print Spooler service is stopped." }
"Stopped Running" { Write-Output "The Windows Update service is stopped, but the Print Spooler service is running." }
"Stopped Stopped" { Write-Output "Both services are stopped." }
Default { Write-Output "The services are in an unknown state." }
}
Another way to do this is to save both services in an array and then use the switch statement to check the status of each service. Note that the Status property is an array in this case, so we can use the [string] class on the entire array to convert the object to a long string. This is different from the previous example, where we made a new string by combining the properties.
# Get the service information for the Windows Update service and the Print Spooler service, and save them in an array
$services = @(
Get-Service -Name "wuauserv"
Get-Service -Name "spooler"
)
# #Because the Status property is not a string, use the [string] class to convert the Status property array into a single string
$ServicesString = [string]$services.Status
# Now use this new string in the switch statement to check the status of the services, and return different messages based on the status.
Switch ($ServicesString) {
"Running Running" { Write-Output "Both services are running." }
"Running Stopped" { Write-Output "The Windows Update service is running, but the Print Spooler service is stopped." }
"Stopped Running" { Write-Output "The Windows Update service is stopped, but the Print Spooler service is running." }
"Stopped Stopped" { Write-Output "Both services are stopped." }
Default { Write-Output "The services are in an unknown state." }
}
String conversion can be a bit tricky, so it is important to understand how to convert the output of a command to a string before using it in a switch statement, and how to verify the output of that string. If possible, use an expression {} on the left side of the switch statement to compare the values directly, instead of converting the values to a string. Remember that all expressions on the left side must return a value of true or false.
Using Expressions
Using expressions is the preferred method of comparing non-string values in a switch statement.
Switch commands will pass the value of the current loop as a pipeline object $_. Use this pipeline object to perform comparisons in an expression block {} on the right side of the switch statement. In most cases a comparison operator (-eq, -ne, -like) solves the problem of non-string values.
The example below uses an expression on the right hand side of the switch. As the switch loops through all of the services, the expression uses the pipeline object to compare the name of the current service to "wuauserv". If the name of the service is "wuauserv", a sub-expression switch will then use the pipeline to check that services status.
If the name of the service is not "wuauserv" or "spooler", the default action will be to continue the loop and check the next service.
# Get the service information for all services
$AllServices = Get-Service
# Create a switch statement to check the name of the service
Switch ($AllServices){
{ $_.Name -eq "wuauserv" } {
Switch ($_.Status){
"Running" { Write-Output "The Windows Update service is running." }
"Stopped" { Write-Output "The Windows Update service is stopped." }
Default { Write-Output "The Windows Update service is in an unknown state." }
}
}
{ $_.Name -eq "spooler" } {
Switch ($_.Status){
"Running" { Write-Output "The Print Spooler service is running." }
"Stopped" { Write-Output "The Print Spooler service is stopped." }
Default { Write-Output "The Print Spooler service is in an unknown state." }
}
}
Default { continue }
}
Conclusion
Switch is a powerful command that allows you to check multiple values and perform different actions based on the values. It's more concise and easier to read than long If/Else statements.
This has been very useful to this author in comparing and identifying specific kinds of objects in Active Directory or Office 365, and executing specific actions based on user, group, or computer objects properties.
I hope this article has helped you understand how to use the Switch statement in PowerShell. What multiple if/else statements can you replace with a switch statement?
Additional Resources
about_Switch (learn.microsoft.com)
Everything you ever wanted to know about the switch statement (learn.microsoft.com)

