PowerShell supports the user to create scripts and execute them to perform various operations. As far as the initial releases are concerned, the interface of PowerShell was like Windows CMD. However, the intermediate versions came with a much better interface. They provided support for .docx files and .txt files, which means that the scripts can be written in Microsoft Office Word or text document. Contrary to these above developments, recent updates on MS-Windows provide a GUI tool support for scriptwriting alongside the terminal. Although, the recent updates of windows have delivered GUI-based tools to write and execute scripts in PowerShell.
For Loops in PowerShell ISE:
The For loops come into play when you want to get the output in repeated form. Different programming languages support loops. The purpose of the loops is the same in all languages, but the syntax is different. Our guide will delve into detail on the For loops in Windows PowerShell ISE.
Syntax
Syntax of For loops may vary from one editor to another. In general, the syntax that will work in PowerShell ISE is given below, and the syntax for loop consists of the following factors:
Initialization: In this part of the loop, the variable is initialized.
Condition: The condition factor decides the termination iteration of the loop. When the condition becomes false, the loop will be terminated:
Updation: This last factor increases or decreases the variable’s value to meet the stopping condition.
Body of the loop or Statements to be executed: This section is the main body of the For loop:
{
statements
}
The image below shows the illustration of the For Loop:
The numbering indicates the chronological order of steps. First, initialization is performed in For loop, so it is given “1“. After the condition is checked, if it is true, the loop’s body will be executed; and if it is false, you will come out of the loop.
How to Work With For Loops in PowerShell ISE
As we have to write the PowerShell Scripts, we will run PowerShell ISE to access the scripting tool. Click on the search icon on your taskbar and look for “PowerShell ISE,” once you get the search result, remember to run it as an Administrator. To open the scripting window, click on the small arrow shown inside the red-colored rectangle:
Basic For Loop:
We will start with the basic example of For loop:
The following program will print the numbers less than 10 using the For loop.
There are two panes in the figure below: the upper one is the Script Pane, and the lower one is the Output Pane. The Script pane is used to write code, whereas the Output pane displays the result of that code. To run the script, press F5 from your keyboard. It will automatically show the result. We have used a simple code for For loop: we have taken a variable $i and initialized it at “1“. Moreover, the stopping condition is “-lt 10“, and the increment at each iteration is set to “1“. Code of this program is written below:
{
Write-Host $i
}
The Script pane is shown below:
To run the script, press F5 from your keyboard, or you can click on the “Run Script” button, as shown below:
The output of the above code is shown in the image below:
Nested For Loops:
Nested For loops contains “For loop within for loop“. We have included two (i and j) variables in this nested For loop. Both variables are assigned “1” as their initial value. There are two loops in the code shown below; one loop is named “1st For loop,” and the nested For loop is represented here by “Nested loop“. Both loops will stop once the variables reach the value “5,” At each iteration, the variable’s value is incremented by “1“.
The code figure of the simple nested For loop program is shown below:
{
"1st For loop = $i"
for ($j=1; $j -lt 5; $j++)
{
"Nested Loop = $j"
}
}
The Script pane is shown below:
Now, run the script (F5) to get the output:
Infinite For Loop:
For loops that are never terminated are known as Infinite For loops. The reason behind their non-termination behavior is that they do not have any termination “condition“. We have taken a simple program. The variable is initialized at “0,” and at every iteration value of the variable is incremented by “1“; while the “condition” portion in this loop is empty.
The source code of the program is shown below:
{
Write-Host "i=$i"
}
The code written in the Script pane is shown below:
The output of the infinite loop code is given below:
Break-in For Loops:
Sometimes, we have to come out of the loop before the termination condition is met. So in such cases, we have to use the “break” statement. For this purpose, we will use “break” in For loop. Earlier, we have discussed infinite loops, the loops which have no stopping criteria. If you want to terminate the infinite loops at any point, you can use “break”; the Script pane of the code is given below, where an endless loop is forced to stop at “15“.
{
Write-Host $i
if ($i -eq 15)
{
break
}
}
The ISE script interface of the code is given below:
The output of the code is shown below. You can see that loop is infinite, but the “break” has forced the loop to terminate at “15“.
Continue in For Loops:
Contrary to the break statements, these statements do not throw you out of the loop but on the execution of continue statements. The process starts from the beginning. For example, the script given below will not print the number “10“, because of the “if” condition. When the number reaches 10, the “if” condition comes true, and the “continue” statement will be executed.
Source code of the above-stated program is given below:
{
if ($i -eq 10)
{
continue
}
Write-Host $i
}
Write-Host "Number 10 is missing"
The image of the codes script is given below:
The output is shown below:
Conclusion
The Windows PowerShell ISE provides you dual support: a command-line interaction and a GUI-based scripting tool. Command-line support works on cmdlets that accept and returns .NET objects to the upcoming command in queue.
This guide provides detailed use of For Loops in PowerShell ISE and several types of For loops are discussed. It is always recommended to start the PowerShell ISE with administrator privileges. Otherwise, you will not get full access to PowerShell.