Conditional Statements

Conditional statements in PowerShell allow you to change the flow of execution in your script.

if, elseIf, and else Statements

if(condition){
   <statement block>
}
elseif(condition2)
{
   <statement block>
}
else
{
   <statement block>
}

If condition evaluates to $true, then PowerShell executes the statement block you provide. Then, it resumes execution at the end of the if / elseif / else statement list. PowerShell requires the enclosing braces around the statement block even if the statement block contains only one statement.

Tip

See the sections “Logical Operators” and “Comparison Operators” for a discussion on how PowerShell evaluates expressions as conditions.

If condition evaluates to $false, then PowerShell evaluates any following (optional) elseif conditions until one matches. If one matches, PowerShell executes the statement block associated with that condition, then resumes execution at the end of the if / elseif / else statement list.

If none of the conditions evaluate to $true, then PowerShell executes the statement block associated with the (optional) else clause, then resumes execution at the end of the if / elseif / else statement list.

switch Statements

switch [options] (expression)
{
   <comparison value> { <statement block> }
   -or-
   { expression }     { <statement block> }

   (…)
   default            { <statement block> }
}

or:

switch [options] –file <filename>
{
   <comparison value> { <statement block> }
   -or-
   { expression }     { <statement block> }

   (…)
   default            { <statement block> }
}

When PowerShell evaluates a switch statement, it evaluates expression against the statements in the switch body. If expression is a list of values, PowerShell evaluates each item against the statements in the switch body. If you specify the –file option, PowerShell treats the lines in the file as though they were a list of items in expression.

The <comparison value> statements allow you to match the current input item against the pattern specified by <comparison value>. By default, PowerShell treats this as a case-insensitive exact match, but the options you provide to the switch statement can change this.

Options supported by PowerShell switch statements

Option

Meaning

-casesensitive

-c

Case-sensitive match.

With this option active, PowerShell executes the associated statement block only if the current input item exactly matches the value specified by <comparison value>. If the current input object is a string, the match is case-sensitive.

-exact

-e

Exact match.

With this option active, PowerShell executes the associated statement block only if the current input item exactly matches the value specified by <comparison value>. This match is case-insensitive. This is the default mode of operation.

-regex

-r

Regular-expression match.

With this option active, PowerShell executes the associated statement block only if the current input item matches the regular expression specified by <comparison value>. This match is case-insensitive.

-wildcard

-w

Wildcard match.

With this option active, PowerShell executes the associated statement block only if the current input item matches the wildcard specified by <comparison value>.

The wildcard match supports the following simple wildcard characters:

?        Any single unspecified character
*        Zero or more unspecified characters
[a-b]    Any character in the range of a-b
[ab]     The specified characters a or b

This match is case-insensitive.

The { expression } statements allow you to process the current input item (stored in the $_ variable) in an arbitrary script block. When it processes an { expression } statement, PowerShell executes the associated statement block only if { expression } evaluates to $true.

PowerShell executes the statement block associated with the (optional) default statement if no other statements in the switch body match.

When processing a switch statement, PowerShell tries to match the current input object against each statement in the switch body, falling through to the next statement even after one or more have already matched. To have PowerShell exit a switch statement after it processes a match, include a break statement as the last statement in the statement block.

For example:

$myPhones = "(555) 555-1212","555-1234"

switch -regex ($myPhones)
{
  { $_.Length -le 8 } { "Area code was not specified"; break }
  { $_.Length -gt 8 } { "Area code was specified" }
  "((555)).*"       { "In the $($matches[1]) area code" }
}

Produces the output:

Area code was specified
In the 555 area code
Area code was not specified

Tip

See the section named “Looping Statements” for more information about the break statement.

By default, PowerShell treats this as a case-insensitive exact match, but the options you provide to the switch statement can change this.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.84.155