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

Question:

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:

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?

Answer:

Another option, if you want to treat the values as numbers instead of strings:

Source:

Is there a way to target multiple conditions with a single switch match in PowerShell? by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply