Learning and Sharing
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
No Result
View All Result

#13 PowerShell Learning: PowerShell Loops

August 14, 2023
in Blog, PowerShell Learning
0
ADVERTISEMENT

Table of Contents

Each programming language or scripting language has several different methods of applying the loop. Looping is a basic programming process that allows us to repeat a command and process large amounts of data. Without this ability, writing scripts would be tedious and almost impossible.

Windows PowerShell supports the following loops:

  • foreach – Executes script block for each item in a collection or array.
  • for – Script block executes a specified number of times.
  • do while – Script block executes as long as condition is True.
  • do until – Script block executes until the condition is True.

ForEach Loop

The simplest and most useful loop is foreach. This is the default alias for ForEach-Object cmdlet. The ForEach loop performs an operation for each item in a collection or array.

PS C:\> Get-Alias foreach

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           foreach -> ForEach-Object

The syntax of ForEach loop is shown below:

foreach ($<item> in $<collection>){<statement list>}

The following commands create a new $arr array and then ForEach loop displays each item of $arr array. Basically, the foreach loop creates a temporary variable $a for every object of $arr array each time the loop executes.

PS C:\> $arr = 100,200,300,400,500
PS C:\> ForEach ($a in $arr) {$a}
100
200
300
400
500
PS C:\> $a
500

The value of every other object  keeps over writing every time the loop executes. So, at the end of loop, the temporary variable $a contains the value of last object. If you look at the content of $a after the loop is completed, you will see value 500 since it is the last member of $arr array.

ForEach loop is one of the most widely used PowerShell loop in scripts.

For Loop

For loop is standard loop which is used to execute a code specific number of times.

The syntax of For loop is shown below:

for (<init>; <condition>; <repeat>)
{<statement list>}
  • The <init> represents one or more commands, separated by commas, that are run before the loop begins. You typically use the <init> portion of the statement to create and initialize a variable with starting value.
  • The <condition> represents the portion of the For statement that resolves to a true or false Boolean value. Windows PowerShell evaluates the condition each time the For loop runs. If the statement is true, the commands in the command block run, and the statement is evaluated again. If the condition is still true, the commands in the statement list run again. The loop is repeated until the condition
    becomes false.
  • The <repeat> represents one or more commands, separated by commas, that are executed each time the loop repeats. Typically, this is used to modify a variable that is tested inside the <condition> part of the statement.

For example, the following command initializes a $i variable with a value 1 and then the test condition is set if the value of $i is less than 5, the value of $i is displayed on screen. The value inside $i is incremented by 1 each time for loop executes. Once the value of $i becomes 5, the test condition evaluates to false thus the For loop exits.

PS C:\> For ($i=1; $i -lt 5; $i++)  {Write-Host $i}
1
2
3
4

So, the value of $i is displayed in console until it is less than 5.

PS C:\> For ($i=1; $i -lt 5; $i++)  {Write-Host $i}
1
2
3
4

While Loop

The While loop runs commands in a command block as long as a conditional test evaluates to true. The While statement is easier to construct than a For statement because its syntax is less complicated. 

In addition, it is more flexible than the ForEach loop because you specify a conditional test in the While statement to control how many times the loop runs.

The following line shows the While loop syntax:

while (<condition>){<statement list>}

When you run a While statement, Windows PowerShell evaluates the <condition> section of the loop before entering the <statement list> section. The condition portion of the statement resolves to either true or false. As long as the condition remains true, Windows PowerShell re-runs the <statement list> section.

The <statement list> section of the loop contains one or more commands that are run each time the loop is entered or repeated.

For example, the following While statement displays the numbers 1 through 5 if the $i variable has not been created or if the $i variable has been created and initialized to 0.

PS C:\> while($i -ne 5) {
>>   $i++
>>   Write-Host $i
>> }
1
2
3
4
5

Do-While and Do-Until Loop

The Do keyword works with the While keyword or the Until keyword to run the statements in a script block, subject to a condition. Unlike the related While loop, the script block in a Do loop always runs at least once.

  • A Do-While loop is a variety of the While loop. In a Do-While loop, the condition is evaluated after the script block has run. As in a While loop, the script block is repeated as long as the condition evaluates to true.
  • Like a Do-While loop, a Do-Until loop always runs at least once before the condition is evaluated. However, the script block runs only while the condition is false.

The syntax:

do {<statement list>} while (<condition>)
do {<statement list>} until (<condition>)

The <statment list> contains one or more statements that run each time the loop is entered or repeated. The <condition> portion of the statement resolves to true or false.

For example, display the value of $i variable in console until it is less than 5.

PS C:\> do {Write-Host $i; $i++}
>> while ($i -lt 5)
1
2
3
4

Do-While loop executes the code block as long as condition is true, but the code block is always executed at least once before testing the condition.

Do-until loop works almost the same as do-while but is executes the code block until the condition is not true.

PS C:\> $i = 1
PS C:\> do {Write-Host $i; $i++}
>> until ($i -gt 5)
1
2
3
4
5

As soon as the $i becomes greater than 5, condition becomes true and do until loop exits.

5/5 - (1 vote)
Previous Post

#12 PowerShell Learning: Hash Tables in Windows PowerShell

Next Post

#14 PowerShell Learning: Conditional Logic in PowerShell

Related Posts

Running Hyper-V and VMware Workstation on The Same Machine

August 15, 2024

How to Uninstall All Autodesk Products At Once Silently

July 29, 2024
Ftr5

How to Uninstall the Autodesk Genuine Service on Windows

July 29, 2024
Ftr19

How to Fix Windows Cannot Read the ProductKey From the Unattend Answer File in VirtualBox

July 26, 2024
Ftr25

How to Update Windows Terminal in Windows 10/11

July 26, 2024

How to Disable The Beep Sound in WSL Terminal on Windows

July 26, 2024

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • How To Turn On uBlock Origin Extension in Chrome (2025)
  • Images Hidden Due To Mature Content Settings In CivitAI
  • Azure OpenAI vs Azure AI Hub, How to Choose the Right One for Your Needs

Categories

Stay in Touch

Discord Server

Join the Discord server with the site members for all questions and discussions.

Telegram Community

Jump in Telegram server. Ask questions and discuss everything with the site members.

Youtube Channel

Watch more videos, learning and sharing with Leo ❤❤❤. Sharing to be better.

Newsletter

Join the movement and receive our weekly Tech related newsletter. It’s Free.

General

Microsoft Windows

Microsoft Office

VMware

VirtualBox

Technology

PowerShell

Microsoft 365

Microsoft Teams

Email Servers

Copyright 2025 © All rights Reserved. Design by Leo with ❤

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory