Using Windows PowerShell as an IT Pro – Part 20

In my last post I finished looking at operators. Now let’s take a look at controlling flow. Up until now most of what we have gone over could be single line commands or commands that are part of a larger script. When you start using flow control then you pretty much have a script on your hands. Of course you don’t have to put any of the code in an actual script file and you can always type\cut and paste it directly into the PowerShell UI (which is what we will show in this blog post).

Like most programming languages, Windows PowerShell allows you to control the flow of your scripts to test and evaluate different conditions. This conditional logic allows you to write dynamic scripts that execute based on the data processed, primarily achieved using the variations of the If statement and different loops. In this blog post I will go over the If statement.

You can use the If statement to run code blocks if a specified conditional test evaluates to true. You can also specify one or more additional conditional tests to run if all the prior tests evaluate to false. Finally, you can specify an additional code block that is run if no other prior conditional test evaluates to true. To explain this further we will use some examples.

This first example uses only a single If statement and does not contain any Elseif statements or any Else statements.

if (6 -gt 4)

{

Write-Host "The value is greater than 4."

}

FlowIf01

In this example, if the If statement is evaluated as false then nothing happens.

For the If statement and other statements in this section, the code has been laid out as if you were writing a script. This means that the code is spread out using multiple lines, making it easier to read. Because the syntax of the statements is free-form in regards to white space (the amount of space between the different components of a command) you can structure your code how you see fit. For example, we could take the code above and place it all on a single line like this:

if (6 -gt 4){Write-Host "The value is greater than 4."}

FlowIf02

In our second example, we added the else statement. This first part of the code executes just as before and the If statement is evaluated as True or False. If it is True, then the Write-Host cmdlet is run like before. By adding the else statement, we can now execute something else if the If statement evaluates to False. In this example, we execute another Write-Host cmdlet.

if (12 -gt 12)

{

Write-Host "The value is greater than 12."

}

else

{

Write-Host "The value is NOT greater than 12."

}

FlowIf03

In our next example, the elseif statement is added. This allows us to evaluate a second condition in addition to the first one. Multiple elseif statements can be used to chain a series of conditional tests so that each test is run only if all the previous tests are false.

if (12 -gt 12)

{

Write-Host "The value is greater than 12."

}

elseif (12 -eq 12)

{

Write-Host "The value is equal to 12."

}

else

{

Write-Host "The value is NOT greater than 12."

}

FlowIf04

In this example we evaluate the first if statement. If it is True, then the first Write-Host cmdlet is executed. If it is False, then the elseif statement is evaluated. If the elseif statement is True, then the second Write-Host cmdlet is executed. If the elseif statement is False, then the else statement is executed and the third Write-Host cmdlet.

In my next post we will look at other flow control statements.

Share

Posted Tuesday, July 12th, 2011 at 1:38 pm by peterl

Tags:

3,431 views

Comments are closed.