Conditional statements in PowerShell allow you to change the flow of execution in your script.
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.
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 [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.
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:
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.
3.144.84.155