Hi,
works here. Can you post a sample of the output?
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I've been fiddling around with the thing below all day, but can't find out why the switch wouldn't work.
Idea is simple, if the username starts with an A we write that it's a match, if it's doesn't we run default and tell that it's not a match.
Would anyone be able to inform me on why this doesn't work? I've went through all the examples, and everything works until I add the -Wildcard.
switch -Wildcard ($env:UserName) { "A*"
{
$env:UserName
write-Host 'Its a match'
}
default {
write-Host 'no match'
$env:UserName
}
}
Answer accepted by question author
Hi,
works here. Can you post a sample of the output?
This works:
"Abc", "XYZ" |
ForEach-Object{
Switch -Wildcard ($_) {
"A*" { "$_ : It's a match"; break }
default { "$_ : no match"; break }
}
}
Note the presence of the "break" at the end of each condition! Your code didn't have that so it'll hit the "default" condition even if the "A*" matched.
Not providing the data you're testing against isn't helpful. Instead of "$_:UserName" try your rest with a known set of string values.
I don't understand, I tested it just now and it didn't work, and now I am running the exact same thing after reopening the file and it works.. I didn't change anything.. Thanks for testing it out anyways, I don't understand why it didn't work, maybe some hidden character or something like that.