*** WARNING! ***
*** This Blog Post will talk about Developer Terms ***
*** ItPros are cautioned to break out their (Patent Pending) ***
*** “Peril Sensitive Sunglasses” (Ala the HitchHikers’ Guide to the Galaxy) ***
*** Before Reading further. If you are amongst the Bold and the DARING and the ***
*** Truly Devil May Care of ITPros, Please feel free to Read on and live Daring! ***

But don’t say I didn’t warn you 🙂
Sean – The Energized Tech


So by now, it seems that some of you have been removing your Peril Sensitive Sunglasses.   Which means either you’ve had too much Jolt Cola, you are TRULY daring ItProfessionals; or you’re starting to get comfortable with some of the terms floating about now.

So now if you HAVEN’T Gone loopy and had your pupils fall out of your head? I do believe you will at least go loopy.

At least I am.  We’re going to talk a little about loops. 

Loops we’ve already done in the past, anybody remember Batch Files in Dos.

“…… Duuuuuh what’s DOS?…”

Ah yes, I forgot.  Modern day ItProfessionals might not remember the hard core days of IRQ’s, Address Lines and COPY CON: to create Batch files.   Batch Files were not for the weak of heart.  vbScript?  Pah!  Give me a FOR %%A and….

Right sorry where was I? Loops!

Loops allow you to repeat tasks automatically until a condition is reached (or if you’re really loopy, Repeat forever.) The latter is referred to as an endless loop.   Also known in the industry as the “endless upgrade cycle”.  ItProfessionals can understand THAT.

So our first loop we’ll look at is the FOREACH-OBJECT ( Very similiar to using a FOR NEXT in Basic or a FOR %%A IN in Batch files ).  Remembering that in Powershell we deal with nothing but Objects, FOREACH-OBJECT is actually very easy to work with once you understand a little of the Syntax.


Ok I see a FEW of you still shy away from THAT Development term.  By the of all this you’ll be cracking jokes in Machine Code, never fear.  FE AD BE AA 00! LOL! LOL!


Never mind, bad joke.  Too “Developery” I see at this time.

So let’s take a look at a typical FOREACH object loop in Powershell.


Where the $PIECE is your reference to WHATEVER line in the OBJECT the FOREACH-OBJECT loop is PRESENTLY LOOKING AT

That’s it.   It’s a very simple loop that goes through an Object and uses it one Entry at a time until it hits the last bits

So let’s see a simple Object we deal with on a regular basis, a Directory and how that would work in a loop.


…and yes this would give us TRULY a “Big Pile of Stuff” but to step through and say LOOK at this directory you would execute a loop like this




All this loop does is Demonstrate a few things.

It will step through the list one Item at a time and show you each object.  It will also show you that as you step through the list, you can access individual properties of those objects like the Name or the CreationTime. 

You can even execute other Commands (including piping).   Almost like a Powershell Script inside a Powershell Script.   I threw the “MORE” onto that one PIPE with GET-MEMBER so you could see a way to pull out properties of an Object like “$THING” within a loop.

And the loop simple runs until “EACH” of the “OBJECTS” are done and “FOR”gotten.

Ok that Joke was a complete stretch but hopefully you got the point.   There are many ways to use the FOREACH-OBJECT, like counting from 1 to something.  Because you can put the Objects in Statically as well.

So to count up (Oh just BECAUSE!)


It can even do the ALPHABET!

FOREACH ( $LETTER in 1..26 ) { [CHAR] ($LETTER+64) }

But really that’s ALL the FOREACH-OBJECT loop does!  It just steps through a list INSIDE an OBJECT until it hits the END of the LIST.   And all you’re using the first Variable for is your Reference to that list.

Remember as well when you’re working with Objects, you always have .Count property, so if you’re working with an Object List, there is Always a way to tell the size of the Object at that time.

Well, we’ll let stew on that for a while.  It looks like the back is getting fidgety and crimping LAN cables with their teeth again.

Until next time, May the Power be with YOU

The Energized Tech