In PowerShell, the for loop is used to iterate over a range of values or elements in a collection. The syntax for a for loop in PowerShell is similar to other programming languages. There are a few variations, but the basic structure is as follows:
Here's a breakdown of each part of the for loop:
Let's look at a few examples to illustrate how for loops work in PowerShell:
This loop will print "Iteration 1" through "Iteration 5". It initializes $i to 1, increments it by 1 in each iteration, and continues as long as $i is less than or equal to 5.
In this example, the loop iterates through each element of the $colors array, printing the name of each color.
This demonstrates a nested loop structure, where an inner loop is inside an outer loop. The output will show combinations of the outer and inner loop variables.
These are basic examples, and you can customize the for loop according to your specific needs in your PowerShell scripts.
The foreach loop is another way to iterate through a collection in PowerShell.
In this example, $collection is the array or collection you want to iterate through. $item is a variable that represents the current item in the iteration. The code within the curly braces {} will be executed for each item in the collection.
In this example, you iterate through $numbers and print the square of them.
Let's consider a scenario where you have a hashtable containing information about employees, and you want to iterate through the hashtable to display each employee's name and department. Here's an example using the foreach loop statement:
The syntax is:
In this example, $employeeData is a hashtable where employee names are keys, and department names are values. The foreach loop iterates through the keys (employee names), and for each employee, it retrieves the corresponding department and prints a message using Write-Output.
The output would be something like:
The foreach-object cmdlet is another way to iterate through a collection in PowerShell.
$collection is the array or collection you want to iterate through. The script block within the curly braces {} contains the code that will be executed for each item in the collection. $_ is a special variable that represents the current item in the iteration.
Note: The difference between foreach-object cmdlet and foreach loop is that results obtained from another cmdlet can be piped through to foreach-object cmdlet which cannot be one with foreach loop.
In this example, you iterate through $numbers and print the square of them.
Let's consider a scenario where you have a list of file names, and you want to display the length of each file using the Get-Item cmdlet and a ForEach-Object loop. Here's an example:
In this example, $fileNames is an array containing the names of three files. The ForEach-Object loop is used to iterate through each file name. Inside the loop, Get-Item cmdlet is used to get information about each file, and then the file length is displayed using Write-Output.
The output would be something like:
Both examples demonstrate how you can use ForEach-Object to process items in a pipeline, performing actions on each item as it passes through the pipeline.

© admincentric, 2023. All Rights Reserved.