Converting VBScript's While...Wend Statement

Definition: Executes a series of statements as long as a given condition is True.


The VBScript While Wend loop is just another way of creating a Do While loop. (You can never have too many ways to create loops, at least not in VBScript.) You can create a While loop in Windows PowerShell by using the - surprise - while statement. While takes two parameters:

  • The loop conditions (in other words, how long do you intend to keep looping).

  • The action to be performed in each iteration of the loop.

In the following example, the value 1 is assigned to the variable $a. A While loop is then established that does two things:

  • Continues to loop as long as the value of $a is less than (-lt) 10.

  • On each iteration of the loop displays the current value of $a, and then increments that value by 1. In Windows PowerShell, the ++ operator increments a value by 1; the syntax $a++ is equivalent to VBScript’s a = a + 1.

Here’s the sample code:

$a = 1
while ($a -lt 10) {$a; $a++}

And here’s what you should get back after running the preceding commands:


Return to the VBScript to Windows PowerShell home page