The keyboard is covered in sweat, there is heavy breathing in front of the screen.  “Quick disconnect the LAN cable before she finds out!”

“Dear Diary.

I should be ashamed.   I am having an affair with vbScript and my Mistress Powershell suspects something is up.   There were a few times this week I accidentally used a ‘ instead of a # to comment.   This is getting dangerous…”


And so I promised.  Today we’re going to get Loopy and see a vbScript loop vs a Powershell loop to make the task of converting easier.   For although many of us love working in Powershell, we must tip our hats in respect to vbScript and it’s amazing established library.

Having said that, we shall RAID and PILLAGE for code to rewrite… “Yeeeaaarrrrrhh!  AHAR!”

So a quick look and one of the most common loops is the For Next where you establish a range of information to step through, going FOR the NEXT one as you encounter it, and well do SOMETHING.

in the magical Land of vbscript here is a very basic looking “For” loop.

For This = 1 to 20

wscript.echo This


in Powershell that would look like this

FOREACH ( $THIS in 1..20) {



There are some SLIGHT differences in the Syntax ( Parentheses about the code in Powershell for one ) but if you look at each, they are very similar.   They both specify a Range of values and simply execute a block of Code.   VbScript requires the “Next” indicate the end of it’s particular FOR loop but the functionality is very similar.

Here’s where the two are VERY much the same (and thus where translation becomes a little bit easier)

Here is a script borrowed from the TechNet Archives (Free to download) which simply lists the Services on a PC

strComputer = "."

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\" & strComputer & "rootcimv2")

Set colRunningServices = objWMIService.ExecQuery("Select * from Win32-Service")
For Each objService in colRunningServices 
Wscript.Echo objService.DisplayName & VbTab & objService.State

My first reaction was “AIHAGHAIHGHAIHGHAHGHAHHG!!!!” with my hands flailing in the air in panic.

Then I looked at that last bit. That looked SUSPICIOUSLY like I was staring at

FOREACH ($ObjService in $ColRunningServices) { Do stuff }

That’s because I was. vbScript can deal with Objects too (not quite as elegantly as Powershell but it can.  Understanding this helps you see a loop like that and realize some of the code is VERY similar

So that same code from vbScript translated to Powershell could look like this


$colRunningServices=GET-WMIOBJECT -namespace ‘root/CIMV2’ –query ‘Select * from Win32-Service’

ForEach ($objService in $colRunningServices) {

  Write-host $objService.DisplayName, $objService.State

Ok the format isn’t EXACTLY the same but this session is about LOOPS. And if you stand back and pause, you’ll see the slight differences and also the similarities between the two loops.

And that’s not really all that difficult to translate between the two.  It’s just about the Syntax.  So next time we’ll see the other loops and hopefully not get TOO loopy and lose our minds.


Just Remember, The Power of Shell is in YOU

The Energized Tech