PowerShell primers

PowerShell for beginners: Scripts and loops

C++ Java PHP .Net Python JavaScript code digital
Thinkstock

PowerShell primers

Show More

Back in 2008, I wrote a piece called PowerShell Tips and Tricks, which covered the then-relatively new Windows scripting language and some cool things you could do with it. Although PowerShell has been important in the Microsoft ecosystem ever since its release, as Windows Server 10 comes closer to release, we find that many features and deployments are significantly easier and more full-featured when carried out with PowerShell. Simply put, learning the language, or at least being familiar with it, is now a must.

PowerShell is built into Windows, so there is no fee or additional licensing cost. In addition, different server products come with their own PowerShells, too, which expands the universe of things you can do with PowerShell.

I have put together a head-start guide to scripting in hopes that many administrators not yet proficient with PowerShell will use this opportunity to improve their skills and be ready for the next wave of Microsoft software.

An introduction to scripts

The basis of PowerShell is script processes and commands. Once we have the framework of creating our own scripts down, we can add in some of the more advanced logic that involves loops and conditionals.

Scripts in PowerShell are basically just text files with the special filename extension of ps1. To create a script, you enter a bunch of PowerShell commands in a sequence in a new Notepad file (or really, you could use any text editor you like), and then save that file as NAME.ps1, where NAME is a friendly description of your script -- with no spaces, of course.

To run a PowerShell script that you already have, you enter in a PowerShell window either:

    • The full path (folder and file name) of the script, like so: c:\powershell\myscripthere.ps1

Or

  • If your script is in the current directory the console is looking at, use a period and then a backslash, like this: .\myscripthere.ps1

Other than that, there is nothing special needed for creating a script in PowerShell. You simply add the commands you like.

Of course, a script probably needs to do more than one thing or you wouldn’t need a script for it. Scripts are common in IT already; administrators have been using login scripts for years to get users' desktops and environments configured consistently every time a user logs on. As technology has gotten better, you can script almost anything, from the bare-metal installation of an operating system on a server fresh out of the factory box all the way up to server workloads, including installing Exchange or file server roles.

We obviously won't go that in depth in this piece, but the basic idea behind a script for our purposes is to do two or three things and then end.

To do that, we need to cover a few elements of a script. The first is the element that can change. The second is the element of the script that actually does the dirty work on everything. Let's look at each of the phases.

Making scripts useful, phase 1: Variables

Now if you buy that the whole point of scripting is to do things over and over again in a consistent way, then you have to buy the idea that you want to do the same action to different things. But how do you change the things that are being acted upon? Variables are how. Variables are kind of like holders, and you can put values, words, numbers -- basically anything -- within them.

In PowerShell, variables always have a dollar sign ($) before them.

Let me declare -- in other words, set up for the first time -- a few variables for you right now:

$name = 'Jon'

$number = 12345

$location = 'Charlotte'

$listofnumbers = 6,7,8,9

$my_last_math_grade = 'D+'

All I have to do to declare those variables is add a dollar sign, then use whatever name for the variable I want -- no spaces are allowed in the name itself -- and then a space, an equals sign, another space and then whatever I want the value of the variable to be.

If I want to have a variable with text as its value, I need to add a single quote on either side of the text. (There are some exceptions to this, but again my goal here is to keep it simple so we’ll stick with this for now.) You can also just declare a variable without putting a value in it. This kind of "reserves" the name, which is probably more helpful when you're in the middle of developing than it is at any other time.

You know what else you can put in a variable? The output of a cmdlet, which is a cute moniker that refers to the simplest bit of .Net-based code you can execute that actually returns a result, either from the PowerShell prompt or from a script. For example, the Get-Process cmdlet lists all processes, while the Get-PSSnapin cmdlet shows all current PowerShell snap-ins that enable new functionality.

To find out the total number of cmdlets available on the system, we can use:

(get-command).count

And at least for the system on which I am writing today's piece, that returned a result of 1,338 cmdlets.

Let's declare a variable to store that count in. We’ll call it

$numberofcmdlets

And let's store in that variable the output of the (get-command).count entry.

$numberofcmdlets = (get-command).count

PowerShell will tell you the current value of any variable if you just type its name into the prompt, so let's see if that worked:

PowerShell for Beginners

Success! Now you can use that variable as part of something else. For a simple example, let's look at the Write-Host cmdlet, which simply writes text to the screen of the machine hosting the PowerShell session. Write-Host has a bunch of capabilities, but in its simplest form, you can just say:

Write-Host "Whatever text I want, as long as it is inside double quotes."

Indeed, you can cut and paste that line into a PowerShell window and it’ll come out exactly like it goes in.

But you can integrate variables with Write-Host. You just call them with the dollar sign notation and work them right into your text. For example, I can say:

Write-Host "There are $numberofcmdlets commands available for use on this system."

And what does PowerShell return to us?

PowerShell for Beginners

Let's put variables aside for now, and move on to the next element of scripting: Decision making and looping.

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.

Do While

Do While is the simplest of the looping constructs in PowerShell. A looping construct is basically a piece of code that does the same action over and over to a set of things -- in other words, it loops through a set of things, doing something to each of them, until some condition changes or that set of things is exhausted.

There are two main types of looping constructs in PowerShell – -- a Do While loop, and another one I will explain in the next section.

Do While is simply a construct that says to PowerShell, "do this to this set of things until some condition I tell you becomes true." It's really as simple as that.

There are two ways to set up this construct. If you want a set of commands to execute at least once, and then as many times as are necessary to satisfy whatever condition you set up, you can simply put Do and a left curly brace on one line, the commands to execute starting on a new line after the left curly brace. And then on a new line, put the right curly brace followed by While and then, within parenthesis, your conditional statement. The conditional statement, again, must be true or false.

For example, let's set up a variable called numbers and give it an initial value of one.

$numbers = 1

Then, let’s set up a simple Do While construct that adds 1 to whatever number is already in that variable, until the variable has the number 10 in it.

Do {

$numbers = $numbers + 1

Write-Host "The current value of the variable is $numbers"

} While ($numbers –lt 10)

Here's what that looks like in the console:

PowerShell for Beginners

You can also set up a Do While construct so that your set of commands only executes when the condition is true. You just need to eliminate the do statement, and only use while.

While ($numbers –lt 10) {

$numbers = $numbers + 1

Write-Host "The current value of the variable is $numbers"

}

ForEach

ForEach is the other looping construct. ForEach simply looks at a set of things and pulls out one at a time to look at them and then, if you say so, perform some type of action or set of commands on it.

Here's how to think of this. Let's say you had a list of users sent over from your HR department, a list of employees who had resigned in the previous quarter. You need to disable their Active Directory accounts. You can use a ForEach loop to work on this. You would say: Dear PowerShell…

Here's my list of users

ForEach (user in that list)

{

disable their log on ability in Active Directory

}

Note the familiar curly braces and their placement. (In developer parlance, what I just showed you above in this example is called pseudocode -- it's a way to break down how you would write code without taking the time to actually figure out the correct syntax, just as a way of making sure you have a good game plan when you approach a problem for which you need to eventually write code to solve.)

One different part of a ForEach loop is the keyword in that lives within that parenthetical statement. That tells PowerShell to create a single variable to hold the values that come out, one at a time, for your bigger set of things.

Let's look at a real code example. Let’s just use a simple set of names in a variable.

$names = "Amy","Bob","Candice","Dick","Eunice","Frank"

When we make a list within a variable, we have created what is known as an array, which is simply a big sort of matrix thing that PowerShell holds in memory that lets it store a bunch of things at one time.

Let's also initialize a count variable so we get a sense of how the loop is going.

$count = 0

Then, let's use a ForEach loop to count how many names we have. Remember our keyword in -- we have to create a new variable that we can call anything we want. This holds each single name that comes out of that list of names we have stored in the variable $names.

I have called this new variable $singlename just to make it clear it is a new variable that just holds a single value that comes out of a list. PowerShell works on that single value and then moves on, grabbing another value from the bigger list, storing it in the new single variable, acting on it based on whatever commands you have put into the loop, and then lathering, rinsing and repeating.

ForEach ($singlename in $names) {

$count += 1

Write-Host "$singlename"

}

The += shorthand basically just says increment the number by whatever interval I put next, in this case 1. I then added a Write-Host line with the $singlename variable so we can get a glimpse into what value PowerShell is working on in the loop.

Finally, I'll add a simple Write-Host line after the end (after the right curly brace, that is) to display the count, so we can actually answer our question.

Write-Host "The total number of names is $count."

Here's what it looks like all put together and run in PowerShell:

PowerShell for Beginners

That's a ForEach loop.

The last word

That is, in a nutshell, how you can begin creating simple scripts and looping constructs in PowerShell. Use this information to build some scripts of your own today!

[For further instruction, check out Microsoft's PowerShell scripting page.]

Copyright © 2015 IDG Communications, Inc.

Bing’s AI chatbot came to work for me. I had to fire it.
Shop Tech Products at Amazon