Avatar of Kelly Garcia
Kelly Garcia
Flag for United Kingdom of Great Britain and Northern Ireland asked on

powershell switch statement

Hi All,

I have written the below switch statement however it does not work, please help:

$a = "lp-lon-5673"
switch ($a) {
    "lp-lon*" {"It is London Laptop."}
    "lp-rom*" {"It is Romania Laptop."}
    {($_ -eq "lp-sing*") -or ($_ -eq "LP-USA*")} {"It is a singapore or USA Laptop"}
    4 {"It is four."}
}

Open in new window

PowershellScripting Languages

Avatar of undefined
Last Comment
Kelly Garcia

8/22/2022 - Mon
Chris Dent

Switch performs a literal match by default. This means your server name would have to be exactly "lp-lon*" to hit the first case.

Switch has a wildcard parameter though.
$a = "lp-lon-5673"
switch -Wildcard ($a) {
    "lp-lon*" {"It is London Laptop."}
    "lp-rom*" {"It is Romania Laptop."}
    {($_ -eq "lp-sing*") -or ($_ -eq "LP-USA*")} {"It is a singapore or USA Laptop"}
    4 {"It is four."}
}

Open in new window

It can also do regular expressions (RegEx) parameter if you need something more advanced.
Kelly Garcia

ASKER
thanks for the code however this don't work:

$a = "LP-USA-12312"
switch -Wildcard ($a) {
    "lp-lon*" {"It is London Laptop."}
    "lp-rom*" {"It is Romania Laptop."}
    {($_ -eq "lp-sing*") -or ($_ -eq "LP-USA*")} {"It is a singapore or USA Laptop"}
    4 {"It is four."}
}

Open in new window

Chris Dent

Same problem, this time with use of the -eq operator. Equality, not a wildcard comparison. Sorry, I didn't see that one first time around.
$a = "LP-USA-12312"
switch -Wildcard ($a) {
    "lp-lon*" {"It is London Laptop."}
    "lp-rom*" {"It is Romania Laptop."}
    {($_ -like "lp-sing*") -or ($_ -like "LP-USA*")} {"It is a singapore or USA Laptop"}
    4 {"It is four."}
}

Open in new window

eq becomes like which supports wildcards.
Your help has saved me hundreds of hours of internet surfing.
fblack61
Qlemo

You might want to switch to regex here, more elegant:
$a = "LP-USA-12312"
switch -RegEx ($a) {
    'lp-lon.*'           {"It is London Laptop."}
    'lp-rom.*'           {"It is Romania Laptop."}
    'lp-sing.*|lp-usa.*' {"It is a singapore or USA Laptop"}
    4                    {"It is four."}
}

Open in new window

ASKER CERTIFIED SOLUTION
Chris Dent

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Aard Vark

While this is already answered, I would encourage you to use break in your switch evaluations. If you do not, your script will continue to evaluate all switch statements. Its more efficient and it can avoid situations where there is potential to match more than 1 result.

switch ("Match me") {
    "Match me" {"1st match"}
    "Match me" {"2nd match"}
    "Match me" {"woe is me it just keeps going"}
    default {"unmatched"}
}

Open in new window


Vs. breaking out of the switch loop.

switch ("Match me") {
    "Match me" {"1st match"; break}
    "Match me" {"2nd match"; break}
    "Match me" {"woe is me it just keeps going"; break}
    default {"unmatched"}
}

Open in new window


Not really a big deal, but I always point it out when people aren't doing it.
Kelly Garcia

ASKER
Thank you LearnCTX, very useful to know.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.