views:

50

answers:

3

I basically want to do this:

switch($someString.ToLower())
{
    "y", "yes" { "You entered Yes." }
    default { "You entered No." }
}
+3  A: 

You should be able to use a wildcard for your values:

switch -wildcard ($someString.ToLower()) 
{ 
    "y*" { "You entered Yes." } 
    default { "You entered No." } 
}

Regular expressions are also allowed.

switch -regex ($someString.ToLower())
{
    "y(es)?" { "You entered Yes." }
    default { "You entered No." } 
}

Powershell Switch documentation: http://technet.microsoft.com/en-us/library/ff730937.aspx

derekerdmann
This is a great solution, although "technically" since I was asking to use separate values, I've marked fletcher as the answer.
Micah
Fair enough, although a different regular expression could probably do the same thing.
derekerdmann
Regex approach would be more concise.
mseery
+2  A: 
switch($someString.ToLower()) 
{ 
    {($_ -eq "y") -or ($_ -eq "yes")} { "You entered Yes." } 
    default { "You entered No." } 
}
fletcher
A: 

Supports entering y|ye|yes and case insensitive.

switch -regex ($someString.ToLower()) {
        "[yes]" {
            "You entered Yes." 
        }
        default { "You entered No." }
}
Doug Finke