Anyone know how I can have a switch statement with multiple possible values like the example below?

switch ($myNumber) {
   1 3 5 7 9 { write-host "Odd" }
   2 4 6 8 10 {write-host "Even" }
}

Used to be easy in VBScript, so I'm sure i'm just missing something simple.

e.g in VBScript

Select Case myNumber 
   Case 1,3,5,7,9
      MsgBox "Odd"
   Case 2,4,6,8,10
      MsgBox "Even"
End Select

Cheers in advance,

Ben

link|improve this question

77% accept rate
feedback

2 Answers

up vote 3 down vote accepted
$myNumber = 3
$arrA = 1, 3, 5, 7, 9
$arrB = 2, 4, 6, 8, 10
switch ($myNumber) { 
    {$arrA -contains $_} { write-host "Odd" } 
    {$arrB -contains $_} { write-host "Even" }
}
link|improve this answer
Thanks its works...I was looking for it since 2 days – Biswajit Apr 13 at 7:06
feedback

In your case you can simply use

switch ($myNumber) {
  { $_ % 2 -eq 1 } { "Odd" }
  { $_ % 2 -eq 0 } { "Even" }
}

An actual attempt to model what you can do there in VB would probably be something like

switch ($myNumber) {
  { 1,3,5,7,9  -contains $_ } { "Odd" }
  { 2,4,6,8,10 -contains $_ } { "Even" }
}
link|improve this answer
I like this one better because it's more familiar to traditional switch statements. – Mark Henderson Apr 13 at 8:49
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.