This blog has moved to http://ThePowerShellGuy.com
Greetings /\/\o\/\/
The Switch statement has an handy option -Regex to use a Regular expresion in it.
the About_Switch help seems missing in Beta 3 but, there is some info in the About_break Help about it.
when you type :
Help about_Break
Part of the Help contains this Example :
In this example, $var is created and initialized to a string value of
"word2". The switch statement uses regex (a regular expression .NET
class) to match the variable value first with the term "word2". Because
the variable value and the first test in the switch statement match,
the first code block in the switch statement runs. When MSH reaches the
first break statement, the switch statement exits. If the four break
statements were removed from the example, then all four conditions
would be met. Thus, this example uses the break statement to display
results when the most specific condition is met.
$var = "word2"
switch -regex ($var)
{
"word2"
{
write-host "Exact" $_
break
}
"word.*"
{
write-host "Match on the prefix" $_
break
}
"w.*"
{
write-host "Match on at least the first letter" $_
break
}
default
{
write-host "No match" $_
break
}
}
This was wat I was looking for in the Dir example in Last post, Hence I came up with the work-around with scriptblocks and -Match.
I found this by using the following command :
MSH>ls $MSHHOME\*.txt | match-string "switch"
about_break.help.txt:5: A statement for immediately exiting foreach, for, while, do, or switch
about_break.help.txt:10: or do loop or in a switch statement, ends the code block. In the case
about_break.help.txt:12: In the case of the switch statement, the break statement causes a code
about_break.help.txt:13: block inside of a switch statement to exit and thus the entire switch
about_break.help.txt:69: A switch statement is not a looping construct, but the break statement
about_break.help.txt:71: met. For example, the following switch statement uses break statements
about_break.help.txt:75: switch -regex ($var)
about_break.help.txt:103: "word2". The switch statement uses regex (a regular expression .NET
about_break.help.txt:105: the variable value and the first test in the switch statement match,
about_break.help.txt:106: the first code block in the switch statement runs. When MSH reaches the
about_break.help.txt:107: first break statement, the switch statement exits. If the four break
about_break.help.txt:119: For information about the switch statement, enter the following
about_break.help.txt:122: help about_switch
about_if.help.txt:35: elseif statements within it, consider using a switch statement instead.
about_if.help.txt:87: For information about the switch statement, enter the following command
about_if.help.txt:90: help about_switch
about_Reserved_words.help.txt:20: foreach while if switch
I did find it this way before in Beta 2 but could not find it yesterday.
enjoy,
gr /\/\o\/\/
Tags : Monad msh