Windows PowerShell 1.0 Flow Control with if, else and elseif

From Techotopia
Jump to: navigation, search
PreviousTable of ContentsNext
Windows PowerShell 1.0 Pipes and RedirectionWindows PowerShell 1.0 Looping with the for and foreach Statements


Purchase and download the full PDF version of this PowerShell eBook for only $8.99

Buy eBook


Since programming is largely an exercise in applying logic, much of the art of script development involves writing scripts that make decisions based on one or more criteria. Such decisions define which code gets executed and, conversely, which code gets by-passed when the script is executing. This is often referred to as flow control since it controls the flow of script execution.

The cornerstone of decision making in the context of Windows PowerShell is the if/else/elseif construct, which is the topic of this chapter.




Using the if Statement

The if statement is perhaps the most basic of flow control options available to the Windows PowerShell developer. Programmers who are familiar with C, C++, C#, Java will immediately be comfortable using Windows PowerShell if statements.

The basic syntax of Windows PowerShell if statement is as follows:

if (boolean expression) {

PowerShell commands to be performed when expression evaluates to true here

}

Essentially if the boolean expression evaluates to $true then the script in the body of the statement is executed. The body of the statement is enclosed in braces ({}). If, on the other hand, the expression evaluates to $false the code in the body of the statement is skipped.

For example, if a decision needs to be made depending on whether one value is greater than another:

$x = 20
$y = 10
if ($x -gt $y)
{
'$x is greater than $y'
}

Clearly, $x is indeed greater than $y, causing the message to appear in the console window.

Using if ... else .. Statements

<google>ADSDAQBOX_FLOW</google> The next variation of the if statement allows us to also specify some code to perform if the expression in the if statement evaluates to false. The syntax for this construct is as follows:

if (boolean expression) {

commands to be executed if expression is true

} else {

commands to be executed if expression is false

}

Using the above syntax, we can now extend our previous example to display a different message if the comparison expression evaluates to be false:

$x = 10
$y = 20
if ($x -gt $y)
{
'$x is greater than $y'
} else {
'$x is less than $y'
}

In this case, the second statement will execute because the value of $x is now less than the value of $y.


Using if ... elseif .. Statements

So far we have looked at if statements which make decisions based on the result of a single logical expression. Sometimes it becomes necessary to make decisions based on a number of different criteria. For this purpose we can use the if ... elseif ... construct, the syntax for which is as follows:

$x = 10
if ($x -eq 20)
{
   '$x is 20'
} elseif ($x -eq 15) 
{
   '$x is 15'
} elseif ($x -eq 10) 
{
   '$x is 10'
} else 
{
   'Value of $x not known'
}

This approach works well for a moderate number of comparisons, but can become cumbersome for a larger volume of expression evaluations. For such situations, the Windows PowerShell switch statement provides a more flexible and efficient solution. For more details on using the switch statement read the chapter entitled The Windows PowerShell 1.0 switch Statement.

Summary

In this chapter we looked at the use of Windows PowerShell if statements including if, if ... else and if ... elseif ... constructs. In the next chapter of this book we will look at looping in Windows PowerShell using for and foreach

<google>BUY_WPS_BOTTOM</google>