Windows Server How-To
How To Create a For Each Loop in PowerShell
Manage and display specific services with this cmdlet.
One of the subjects that administrators who are new to PowerShell sometimes have difficulty with is ForEach loops. That being the case, I wanted to take the opportunity to explain how these loops work.
A ForEach loop is generally used to perform some sort of action against the individual items within a data set. The data set can be anything. It might consist of a list of processes, a list of disks, or even a list of servers. That being the case, the first thing that you will have to do if you want to use a ForEach loop is to define the data set by mapping the data to a variable. The way that you would do this varies depending on whether the data set will be made up of items that you are hard coding, or if it will be composed of items resulting from a PowerShell query.
To show you what I mean, let's pretend that we wanted to create a data set containing the names of system processes, and we wanted to assign those processes to a variable named $Proc. If we wanted to query Windows for the process list and assign that list to a variable, we could do so with this command:
$Proc = Get-Process
If on the other hand, we wanted to hard code some process names, we could do so with this command:
$Proc = @("winlogon","spoolersc","smss")
In the line above, the @ symbol tells PowerShell that we are creating an array.
The key to understanding how ForEach loops work in PowerShell is to understand what the variables do. There are two variables that you need to know about. The first of these variables is the one that I just talked about (in this case, it's $Proc). This variable represents the entire data set.
The other variable that you will need to use represents one individual item at a time from the data set. The thing that tends to confuse people about this variable is that you don't actually have to declare it. You can just start using it, without having to do anything first. Those who are unfamiliar with ForEach loops often wonder where the variable came from and what it does.
I am going to show you a code sample in a moment, but before I do, let me tell you which variable names I will be using. As previously mentioned, my sample block of code will be based around Windows processes. The $Proc variable will contain the name of every process that is running on my computer. If you look at Figure 1, you can see how I have assigned this variable and what it looks like when I tell PowerShell to show me the variable's contents.
The variable that I will use to reference individual Windows processes will be called $IndProc. This isn't a name that is reserved by PowerShell or anything like that. It's just a name that I made up. As I said before, this variable does not even have to be declared.
So with that said, we can create the loop with a single line of code. We are going to be telling PowerShell that we want to create a ForEach loop that looks at each $indProc in $Proc (each individual list of processes in the process list). After that, we enclose the code to be executed as a part of the loop in a set of braces. In this case, the loop would look like this:
ForEach ($indProc in $Proc)
This loop doesn't do anything because there is no code between the braces, but the code does technically create a loop. If we wanted to create a list of process names without all of the extra data shown in Figure 1, we could do something like this:
ForEach ($IndProc in $Proc)
Of course this code would need to exist within a script rather than being typed in directly at the command line. In fact, many administrators like to place these types of code blocks into PowerShell functions.
Brien Posey is a 20-time Microsoft MVP with decades of IT experience. As a freelance writer, Posey has written thousands of articles and contributed to several dozen books on a wide variety of IT topics. Prior to going freelance, Posey was a CIO for a national chain of hospitals and health care facilities. He has also served as a network administrator for some of the country's largest insurance companies and for the Department of Defense at Fort Knox. In addition to his continued work in IT, Posey has spent the last several years actively training as a commercial scientist-astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space. You can follow his spaceflight training on his Web site.