Switch

When you need to test multiple conditions, a Switch statement could become handy. It's a better way then using If Elseif Elseif ... Elseif Else conditions.

Usage of switch

switch (expression) {
	(evaluated expression) {
		# your code here
	}
	value {
		# your code here
	}
	default {
		# your code here
	}
}
		

The evaluated expression is an expression that need to be tested and checked first.

The value could be a number or a string.

Default should be written because it catches everything that hasn't been checked (if previous expressions were False).

Examples

Example with evaluated expressions:

switch (5) {
    (1 + 4) {
        Write-Host "Congratulations, you applied addition correctly"
    }
    (1 + 5) {
        Write-Host "This script block better not run"
    }
    (6 - 1) {
        Write-Host "Congratulations, you found the difference correctly"
	}
    (1 - 6) {
        Write-Host "This script block better not run"
    }
}
		

A basic Switch statement:

$a = 5

switch ($a) { 
    1 {"The color is red."}
    2 {"The color is blue."}
    3 {"The color is green."}
    4 {"The color is yellow."}
    5 {"The color is orange."}
    6 {"The color is purple."}
    7 {"The color is pink."}
    8 {"The color is brown."}
    default {"The color could not be determined."}
}
		

Switch Statement using wildcards.

With -wildcard we say to the Switch statement that we are about to use wildcards. Don't forget to use the asterisk (*). It means any character and we don't care how many.

$a = "d14151"

switch -wildcard ($a) { 
    "a*" {"The color is red."} 
    "b*" {"The color is blue."} 
    "c*" {"The color is green."} 
    "d*" {"The color is yellow."} 
    "e*" {"The color is orange."} 
    "f*" {"The color is purple."} 
    "g*" {"The color is pink."}
    "h*" {"The color is brown."} 
    default {"The color could not be determined."}
}
		

Again a Switch structure with wildcards but we don't use the asteriks (*) now but a question mark. A question mark (?) means "Any one character", only 1 character but we don't care what that character is.

$a = "d14151"

switch -wildcard ($a) { 
    "?14150" {"The color is red."} 
    "?14151" {"The color is blue."} 
    "?14152" {"The color is green."} 
    "?14153" {"The color is yellow."} 
    "?14154" {"The color is orange."} 
    "?14155" {"The color is purple."} 
    "?14156" {"The color is pink."}
    "?14157" {"The color is brown."} 
    default {"The color could not be determined."}
}
		

Switch Statement using Regex:

Don't forget the -regex to alert our Switch that we are about to use a regular expression.

$a = "r14151"

switch -regex ($a) { 
    "[a-d]" {"The color is red."} 
    "[e-g]" {"The color is blue."} 
    "[h-k]" {"The color is green."} 
    "[l-o]" {"The color is yellow."} 
    "[p-s]" {"The color is orange."} 
    "[t-v]" {"The color is purple."} 
    "[w-y]" {"The color is pink."}
    "[z]" {"The color is brown."} 
    default {"The color could not be determined."}
}
		

Leaving our Switch when we found a match.

To exit our Switch Statement we can use the break statement.

$a = "14151"

switch -regex ($a) { 
    "\d{8}" {"The color is red."; break} 
    "\d{7}" {"The color is blue."; break} 
    "\d{6}" {"The color is green."; break} 
    "\d{5}" {"The color is yellow."; break} 
    "\d{4}" {"The color is orange."; break} 
    "\d{3}" {"The color is purple."; break} 
    "\d{2}" {"The color is pink."; break}
    "\d{1}" {"The color is brown."; break} 
    default {"The color could not be determined."}
}
		

Switch Statement using Arrays:

$a = 21, 38, 6

switch ($a) { 
    1 {"The color is red."} 
    2 {"The color is blue."} 
    3 {"The color is green."} 
    4 {"The color is yellow."} 
    5 {"The color is orange."} 
    6 {"The color is purple."} 
    7 {"The color is pink."}
    8 {"The color is brown."} 
}
		

PowerShell will evaluate the values of the array and will check if any of them has a match. Our result would be "The color is purple." .