Is there a way to target multiple conditions with a single switch match in PowerShell?

Viewed 2500

In an example script, I want to execute one block of code if my variable matches 1, another one if it matches 2, 3, 5, or 8, and a different block for 4, 6, or 7.

I'd like to do something like this:

switch($x)
{
    1 {'Condition 1'}
    2 -or 3 -or -5 -or 8 {'Condition 2'}
    4 -or 6 -or 7 {'Condition 3'}
}

But this doesn't work. Is there a way to do this sort of work with switch, without having to spell out all 8 options individually, or are multiple if statements the only way to go?

2 Answers
Related