PowerShell primers

PowerShell for beginners: Scripts and loops

PowerShell primers

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

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.

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