Windows PowerShell supports the conditional statement if/elseif/else, which branches execution based on a condition, and the switch statement, which can handle multiple complex conditions.
The if/elseif/else Statement
The if/elseif/else statement allows you to
execute a block of code if a specified condition is met. However, the
statement can also execute a block of code if a condition is not met.
This statement uses comparison operators to test the condition.
The following is a simple if/elseif/else statement.
PS > $url = "http://SPServer01"
PS > if((Get-SPSiteAdministration $url).DiskUsed -gt 20MB) {
>> "Disk space used is more than 20 MB"
>> } else {
>> "Disk space used is less than 20 MB"
>> }
>>
Disk space used is less than 20 MB
In this example, we use the Get-SPSiteAdministration cmdlet to retrieve the size (the amount of disk space used) of a site collection. We then use an if/else
statement to check if this size exceeds 20MB. If so, we return “Disk
space used is more than 20MB.” If the condition is not met, we return
“Disk space used is less than 20MB.”
The next example demonstrates the use of the elseif keyword within the if/elseif/else statement.
PS > $url = "http://SPServer01"
PS > if((Get-SPSiteAdministration $url).DiskUsed -gt 20MB) {
>> "Disk space used is more than 20 MB"
>> } elseif((Get-SPSiteAdministration $url).DiskUsed -gt 10MB) {
>> "Disk space used is more than 10 MB"
>> } else {
>> "Disk space used is less than 10 MB"
>> }
>>
Disk space used is more than 10 MB
The elseif keyword lets you introduce another condition and a corresponding additional execution branch. You can add any number of elseif keywords to an if/elseif/else statement.
The switch Statement
The switch statement is a series of if statements and is used to evaluate a condition against a number of potential matches. The switch
statement matches the expression with each of the conditions, and if a
match is found, an action associated with the condition is performed.
If more than one condition applies, the switch statement will execute each of the applicable conditions.
The following is a simple switch statement.
PS > $a = 1
PS > switch($a) {
>> 1 { "contains one" }
>> 2 { "contains two" }
>> }
>>
contains one
In this example, we choose an action based on the value in parentheses after the switch
keyword. The value is matched with each of the conditions. If a match
is found, the action associated with that condition is performed.
The default comparison operator used by the switch statement is the -eq operator. It is possible to use other operators when using the switch statement, as the next example demonstrates.
PS > $url = "http://SPServer01"
PS > switch((Get-SPSiteAdministration $url).DiskUsed) {
>> {$_ -gt 20MB} { "Disk space used is more than 20 MB"; Break }
>> {$_ -gt 10MB} { "Disk space used is more than 10 MB"; Break }
>> {$_ -gt 5MB} { "Disk space used is more than 5 MB"; Break }
>> Default { "Disk space used is less than 5 MB" }
>> }
>>
Disk space used is more than 10 MB
In this example, the values in parentheses are the
amount of disk space used. The value is then matched against the
patterns in each clause, and if a match is found, the corresponding
clause is executed. Since a Break is used at the end of each condition,
the switch stops as soon as a match is made. The Default clause is used to perform an action if none of the switch values match the pattern. Notice how we use the $_ variable to reference the input object.
The switch statement supports a couple of options that you can use to control the pattern matching. By default, the switch statement is case-insensitive. You can perform a case-sensitive pattern match with the -Casesensitive option.
PS > $url = "http://SPServer01"
PS > switch -CaseSensitive ($url) {
>> "http://SPServer01" {"matches http://SPServer01"; Break }
>> "http://SPSERVER01" {"matches http://SPSERVER01"; Break }
>> default {"no match found"}
>> }
>>
matches http://SPServer01
We can also use wildcard pattern matching with the switch statement. Here is an example:
PS > $url = "http://SPServer01"
PS > switch -WildCard -CaseSensitive ($url) {
>> "http*[S]*" {"Starts with 'http' and contains a upper-case S"; Break }
>> "http*[s]*" {"Starts with 'http' and contains a lower-case s"; Break }
>> }
>>
Starts with 'http' and contains a upper-case S
In this example, first we check if the value of the variable $url starts with a 'http' and contains an uppercase S. Then we check if the value of the variable $url starts with 'http' and contains a lowercase s. Since the first pattern matches the variable, the corresponding clause is executed.
The switch statement also supports regular expressions, which let you create complex pattern matches. Here is an example:
PS > $url = "http://SPServer01"
PS > switch -regex ($url) {
>> "^(http|https):/{2}" {"match found"}
>> }
>>
match found
In this example, we test if the value of the variable $url starts with 'http' or 'https' followed by the : character and two / characters.