Windows Powershell Switch Statement _powershell

Source: Internet
Author: User

The following example converts IF-ELSEIF-ELSE into a switch statement

Copy Code code as follows:

# Use If-elseif-else
If ($value-eq 1)
{
"Beijing"
}
Elseif ($value-eq 2)
{
"Shanghai"
}
Elseif ($value-eq 3)
{
"Tianjin"
}
Else
{
"Chongqing"
}

# Use Switch
Switch ($value)
{
1 {"Beijing"}
2 {"Shanghai"}
3 {"Tianjin"}
4 {"Chongqing"}
}

1

Test range of values

When using Switch, the default comparison operator is-eq equals, you can also customize the comparison conditions, put the conditions in curly braces, you must ensure that the return value of the conditional expression is a Boolean type "$True" or "$False"

Copy Code code as follows:

$value =18
# Use the Switch to test the range of values
Switch ($value)
{
{$_-lt 10} {"Less than 10"}
10 {"Equals 10"}
{$_-GT 10} {"Greater than 10"}
}
#输出
#大于10

No matching Criteria

In a if-else statement, if there is no appropriate matching of conditions, it can be handled in else, also in the switch statement if there is no conditional match in the case, you can use the keyword default for processing.
The same is the case above, slightly modified:

Copy Code code as follows:

$value =-7
# Use the Switch to test the range of values
Switch ($value)
{
{($_-lt)-and ($_-gt 0)} {"Less than 10"}
10 {"Equals 10"}
{$_-GT 10} {"Greater than 10"}
Default {"No Matching Criteria"}
}
#Output:
#没有匹配条件

Multiple Criteria Matching

If more than one condition in the case matches, then each matching condition is processed, for example:

Copy Code code as follows:

$value =2
# Use the Switch to test the range of values
Switch ($value)
{
{$_-lt 5} {"Less than 5"}
{$_-GT 0} {"Greater than 0"}
{$_-LT 100} {"Less than 100"}
Default {"No Matching Criteria"}
}

#小于5
#大于0
#小于100

If you encounter matching criteria only once, you can use the break keyword

Copy Code code as follows:

$value =99
# Use the Switch to test the range of values
Switch ($value)
{
{$_-lt 5} {"Less than 5"; break}
{$_-GT 0} {"Greater than 0"; break}
{$_-LT 100} {"Less than"; break}
Default {"No Matching Criteria"}
}

#大于0

Comparing strings

The previous condition compares the numbers, then compares the string, the default condition judgment is-eq, we know that the use of strings in PowerShell is not sensitive to the-eq of the case, so the following example is available:

Copy Code code as follows:

$domain = "Www.jb51.net"
Switch ($domain)
{
"Www.jB51.net" {"OK 1"}
"Www.JB51.net" {"OK 2"}
"WWW.jb51.NET" {"OK 3"}
}
Ok 1
Ok 2
Ok 3

Case sensitive

How to revert to case sensitive mode when comparing strings, switch has a-case option, and once this option is specified, the comparison operator switches from-eq to-CEQ, which is the case sensitive comparison string:

Copy Code code as follows:

$domain = "Www.jb51.net"
#大小写敏感
Switch-case ($domain)
{
"Www.jB51.net" {"OK 1"}
"Www.JB51.net" {"OK 2"}
"Www.jb51.net" {"OK 3"}
}
#Ok 3

Using wildcard characters

String is very special, but using wildcards, fortunately PowerShell also support, really power ah. However, to specify the-wildcard option after the switch statement

Copy Code code as follows:

$domain = "Www.jb51.net"
#使用通配符
Switch-wildcard ($domain)
{
"*" {"Match ' *"}
"*.net" {"Match *.net"}
"*.*.*" {"Match *.*.*"}
}
Match ' * '
Matching *.net
Matching *.*.*

In string matching, more powerful than wildcard features are regular expressions, and PowerShell switch statements are also supported, which is fantastic. Of course, you need to specify options for the Switch keyword-regex

Copy Code code as follows:

$mail = "Www@jb51.net"
#使用通配符
Switch-regex ($mail)
{
"^www" {"www start"}
"net$" {"Net End"}
' d{1,3}.d{1,3}.d{1,3}.d{1,3} ' {' IP address '}
}

#www打头
#net结尾

Processing multiple values at the same time

The switch supports matching all elements of the collection, and the following example uses the PowerShell switch statement to demonstrate the number of daffodils printed:

Copy Code code as follows:

$value =100..999
Switch ($value)
{
{[Math]::P ow ($_%10,3) +[math]::P ow ([Math]::truncate ($_%100/10), 3) +[math]::P ow ([Math]::truncate ($_/100), 3)-eq $_} {$_}
}

#153
#370
#371
#407

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.