PowerShell primers

PowerShell for beginners: Scripts and loops

PowerShell primers

Show More
1 2 3 4 5 6 Page 4
Page 4 of 6

Making scripts useful, phase 2: If/Then, Do-While and ForEach

The next phase actually lets you do some magic. We know how to store values in variables now, but we need to do some things to those variables. Let's take a look.

If/Then

The simplest form of decision making in PowerShell is the if/then mechanism; in PowerShell lingo, this is called the "construct." It basically works like this:

If something is some comparison to something else--> Then do this action.

You format it by putting your comparison in parenthesis, putting a left curly brace alone on a new line, adding the PowerShell cmdlets or actions to perform if that action is true beginning on another new line, and then ending with a right curly brace on a final new line. The key points here are that:

  • The comparison statement must have a logical response of either TRUE or FALSE. Think of it as a yes or no question. If you need to do something not based on a yes or no question, then another loop construct is appropriate; we'll cover that in a bit.
  • The code that runs if your statement is YES or NO must be within curly braces, and it is best practice to put these curly braces on lines by themselves so that you can match them up when you are writing more complicated scripts.

For example, if I wanted to compare two numbers -- let's say 5 and 10 -- and have PowerShell display whether 10 was greater than 5, then we could write the following if/then construct:

If (10 –gt 5)

{

Write-Host "Yes"

}

You may already know that –gt is the PowerShell switch for "greater than." We used Write-Host in the previous example as well.

If we run this at a PowerShell prompt, we get:

PowerShell for Beginners

That's kind of simple and probably not going to be a ton of use for you in your administrative duties. To make the construct a little more functional, you can add more "nests" to the If/Then block. Again, these execute in a progression -- in programming parlance, this is known as a serial construct, meaning one comparison has to finish, then the next one, and as soon as one finishes, the comparisons stop.

It would look like this:

If (10 –gt 11)

{

Write-Host "Yes"

} elseif (11 –gt 10)

{

Write-Host "This time, yes"

}

You should be able to follow that one pretty easily; here is the result.

PowerShell for Beginners

The first logical comparison (is 10 greater than 11? No) is false, so PowerShell moves on to the next one via the elseif comparison, which is PowerShell parlance for "next, please!" (is 11 greater than 10? Yes), and prints the Write-Host line I wrote for that test.

In constructs like these, when you're testing, it's best to have different output for each test. If I had chosen Yes for both constructs, and then run the script, I would not have been able to tell which comparison test was producing the Yes -- there's no differentiation. Here, I added "This time" so I could tell what was actually happening in the script.

You can include a bunch of these elseif blocks in your script -- theoretically there is no maximum. It's a great way to establish conditions before you do something. For instance, if I wanted to move mailboxes only where the user's region was in the United States, then I could use an IF statement to get at the mailbox properties and then write the code for the move within the curly braces.

Or maybe I have a machine with a pesky startup problem because of an interaction with an old piece of software, and so I need to write a script that I set off as a scheduled task that checks a service after a minute or two and, if it is stopped (there's my comparison), starts the service (there's my code).

Hopefully you can see the applications of this type of PowerShell construct.

Finally, you can choose to include an else block in your if/then construct, which runs as basically the alternative ending for your script -- if all of the ifs and elseifs do not actually evaluate and run their code, then the else block can do something to conclude the script. The else block is written at the very end and DOES NOT include a parenthetical comparison statement; you leave it off.

Here's an example: I might make a series of comparisons like this, and then make a statement of exasperation at the end:

If (10 –gt 11)

{

Write-Host "Yes"

} elseif (11 –lt 10)

{

Write-Host "This time, yes"

} elseif (20 –gt 40)

{

Write-Host "Third time was a charm"

} else {

Write-Host "You're really terrible at math, aren’t you?"

}

If I run this in PowerShell, this is what I get back from the console:

PowerShell for beginners

That's if/then constructs in a nutshell.

1 2 3 4 5 6 Page 4
Page 4 of 6
7 inconvenient truths about the hybrid work trend
Shop Tech Products at Amazon