Here’s one way to loop your code with PowerShell. PowerShell looping is one of the easiest ways to loop through processes in code and a good way to start learning to iterate little processes you develop.

A rather typical procedure to learn early or it won’t make much sense when down the rabbit hole. I like how this code writes and teaches you how to iterate rather quickly. Be sure to paste and hit play.

$k= New-Object -comobject "Wscript.Shell"

$a=1
$b=5
$n="`n"

Start-Sleep -seconds $b

DO
{
$k.sendkeys("1 of $a$n")
$a++
Start-Sleep -seconds $b
} While ($a -le 5)

There are lots of ways to do things in code. Maybe you loop things differently?

But PowerShell keeps it rather simple.

When you’re wanting to loop text output, you can do so easily with Powershell using a DO WHILE.

do while loop in powershell-Windows PowerShell ISE

Here’s a loop we can use to repeat stuff in PowerShell.

Found the beginning of this lesson here, meaning I found code, copy pasted it, and it was enough to help me learn every aspect of the code. The lesson @ the Microsoft website is a little more extreme and doesn’t explain the line by line code, which I feel needs an extra explanation.

Line 1
$k= New-Object -com "Wscript.Shell"

New-Object Creates an instance of a Microsoft .NET Framework or COM object.

New-Object Opens the ability to send a keystroke within PowerShell.

We call this with $k moving forward.

Like in math class, you used X to represent a value or Y to represent a value.

This makes our code easier to write and read.

$k.sendkeys, later in the script, is now easier.

-comobject or -com for short is a special ability.

The -Comobject Specifies the programmatic identifier (ProgID) of the COM object. Luckily you don’t need to understand what this means.

Line 3,4,5
$a=1
$b=5
$n="`n"

Like in math class, you have X representing a value and Y representing a value.

We will use these extra variables later in the tutorial.

Today, $a and $b represent a tiny lesson, which is.. you can leave unused code.

$n="`n"

This tells PowerShell to make a new line whenever we say $n in our code. So when we output keyboard key strokes of “something $n” — powershell essentially hits “ENTER” on the keyboard. This is a powerful next step for anyone looking to automate their UI.

Line 7
Start-Sleep -seconds $b

Ever tried living without sleeping? Writing UI automation is the same. You need breaks, waits, pauses, etc.

In powershell, it’s called Start-Sleep -seconds X…

Line 9,10,11,12,13,14
DO
{
$k.sendkeys("1 of $a$n")
$a++
Start-Sleep -seconds $b
} While ($a -le 5)

Here’s the full loop, a beautiful little tool that we can use to do very quick simple tasks, or loop massive dynamic scripts. Maybe it emails someone something every 15 minutes. Like a scheduler, you can pad a pause into this loop and expect the loop to repeat at the speed you desire.

So, not only are we saying something with our sendkeys tool, but we are also padding a pause into the tool.

If you want to learn more about the sendkeys tool, I built a blog called Make Your Computer Type For You in Powershell. Where I discuss that tool more. Let’s continue.

Line 9,10,12,14
DO
{
$a++
} While ($a -le 5)

DO this work until X equals 5.

-lt is less than, and -gt is greater than. -le seems to be another operand and I cant seem to google it today. what’s nice is this code takes a split second to run. Test and use what works best for you.

$a++ is thing that increases. Per loop.

So the first run, makes the $a=1.

The second run, makes the $a=2.

The third run, makes the $a=3

4th, $a=4

5th, $a=5. Now the condition at the end stops the loop and continues reading the rest of the code.

After it repeats the loop 5 times.

It knows it increases once because of the $a++ logic we built.

$a++ effectively increases, +1, each time. It also helps us stop our loop from running forever. If you remove $a++ the loop will keep running into you end the script. Which means this could be a bit risky when pointed at the wrong or right thing.

And the condition…

1 -le 5

2 -le 5

3 -le 5

4 -le 5

5 -le 5

END… now the rest of your code would be computed.

 

%d bloggers like this: