SlideShare a Scribd company logo
Windows PowerShell Basics – For Loop
i | P a g e
Table of Contents
Overview.......................................................................................................................................................1
Applies To..................................................................................................................................................1
Pre-Requisites ...........................................................................................................................................1
PowerShell Script – Function ........................................................................................................................1
Code Snippet – For loop............................................................................................................................1
PowerShell Output – For Loop..............................................................................................................2
Windows PowerShell Basics – For Loop
1 | P a g e
Overview
PowerShell functions are helpful in saving time when you have repetitive task(s) to be executed. In this
guide we will demonstrate, as to how to construct a “for” loop with different incremental and
decremented values.
Applies To
Tested on Windows 10, Windows 2008 R2 and Windows 2012.
Pre-Requisites
Launch PowerShell Command Console or PowerShell ISE.
To run this script, Execution Policy should be set to either of these “AllSigned” or “RemoteSigned” or
“Unrestricted”, you can get current execution policy by running the command; “Get-ExecutionPolicy”.
Each Policy type and its purpose is shown in the below table.
Policy Type Purpose
Restricted No scripts can be run. Windows PowerShell can be used only in interactive mode.
AllSigned Only scripts signed by a trusted publisher can be run.
RemoteSigned Downloaded scripts must be signed by a trusted publisher before they can be run.
Unrestricted No restrictions; all Windows PowerShell scripts can be run.
PowerShell Script – Function
This PowerShell script demonstrates the usage of for loop in a PowerShell.
Code Snippet – For loop
The code snippet is for demonstrating “for” loop usage, wherein incremental and decrementing values
are demonstrated.
Clear-Host
Write-Host "`nIncrement by 1 for loop" -BackgroundColor Green -ForegroundColor White
#
# Increment by 1
#
for($Iterator=0; $Iterator -le 5; $Iterator++) {
Write-Host "Increment by 1 - Current Incremental Value " $Iterator
[console]::Beep(65, 100)
}
Windows PowerShell Basics – For Loop
2 | P a g e
#
# Skip +2 or Increment by 2
#
Start-Sleep -Seconds 2
Write-Host "`nIncrement by 2 for loop" -BackgroundColor Green -ForegroundColor White
for($Iterator=0; $Iterator -le 10; $Iterator += 2) {
Write-Host "Increment by 2 - Current Incremental Value" $Iterator
[console]::Beep(37, 100)
}
#
# Decrement by 1
#
Start-Sleep -Seconds 2
Write-Host "`nDecrement by 1 for loop" -BackgroundColor Green -ForegroundColor White
for($Iterator=0; $Iterator -ge -5; $Iterator--) {
Write-Host "Decrement by 1 - Current Decremented Value" $Iterator
[console]::Beep(37, 100)
}
#
# Skip -2 or Decrement by 2
#
Start-Sleep -Seconds 2
Write-Host "`n Skip -2 or Decrement by 2 for loop" -BackgroundColor Green -ForegroundColor White
for($Iterator=0; $Iterator -ge -10; $Iterator -= 2) {
Write-Host "Decrement by 2 - Current Decremented Value" $Iterator
[console]::Beep(37, 100)
}
PowerShell Output – For Loop
When script is executed; below output will be displayed.

More Related Content

What's hot (20)

PDF
How To Check and Delete a File via PowerShell
VCP Muthukrishna
 
PDF
How To Check file exists and Delete PowerShell
VCP Muthukrishna
 
PDF
How To Create Power Shell Function Mandatory Parameter Value
VCP Muthukrishna
 
PDF
How To Install and Configure Open SSH Server on Ubuntu
VCP Muthukrishna
 
PDF
Bash Script - How To Monitor Application Error Logs and Send Notification
VCP Muthukrishna
 
PDF
Install and Configure RSyslog – CentOS 7 / RHEL 7
VCP Muthukrishna
 
PDF
Zimbra Troubleshooting - Mails not being Delivered or Deferred or Connection ...
VCP Muthukrishna
 
PDF
How To Install and Configure SNMP on RHEL 7 or CentOS 7
VCP Muthukrishna
 
PDF
How To Setup SSH Keys on CentOS 7
VCP Muthukrishna
 
PDF
How To Create PowerShell Function
VCP Muthukrishna
 
PDF
How To Connect Amazon AWS EC2 with Key Pair – Linux
VCP Muthukrishna
 
PDF
How To List Nginx Modules Installed / Complied on CentOS 7
VCP Muthukrishna
 
PDF
How To Install and Configure GNome on CentOS 7
VCP Muthukrishna
 
PDF
How To View Current Execution Policy PowerShell
VCP Muthukrishna
 
PDF
How To Install and Configure Apache SSL on CentOS 7
VCP Muthukrishna
 
PDF
How To Manage Linux User on RHEL 7
VCP Muthukrishna
 
PDF
Shell Script Disk Usage Report and E-Mail Current Threshold Status
VCP Muthukrishna
 
PDF
Batch script for nslookup range of ip address
VCP Muthukrishna
 
PDF
Install Active Directory PowerShell Module on Windows 10
VCP Muthukrishna
 
PDF
How to Fix Duplicate Packages in YUM on CentOS 7
VCP Muthukrishna
 
How To Check and Delete a File via PowerShell
VCP Muthukrishna
 
How To Check file exists and Delete PowerShell
VCP Muthukrishna
 
How To Create Power Shell Function Mandatory Parameter Value
VCP Muthukrishna
 
How To Install and Configure Open SSH Server on Ubuntu
VCP Muthukrishna
 
Bash Script - How To Monitor Application Error Logs and Send Notification
VCP Muthukrishna
 
Install and Configure RSyslog – CentOS 7 / RHEL 7
VCP Muthukrishna
 
Zimbra Troubleshooting - Mails not being Delivered or Deferred or Connection ...
VCP Muthukrishna
 
How To Install and Configure SNMP on RHEL 7 or CentOS 7
VCP Muthukrishna
 
How To Setup SSH Keys on CentOS 7
VCP Muthukrishna
 
How To Create PowerShell Function
VCP Muthukrishna
 
How To Connect Amazon AWS EC2 with Key Pair – Linux
VCP Muthukrishna
 
How To List Nginx Modules Installed / Complied on CentOS 7
VCP Muthukrishna
 
How To Install and Configure GNome on CentOS 7
VCP Muthukrishna
 
How To View Current Execution Policy PowerShell
VCP Muthukrishna
 
How To Install and Configure Apache SSL on CentOS 7
VCP Muthukrishna
 
How To Manage Linux User on RHEL 7
VCP Muthukrishna
 
Shell Script Disk Usage Report and E-Mail Current Threshold Status
VCP Muthukrishna
 
Batch script for nslookup range of ip address
VCP Muthukrishna
 
Install Active Directory PowerShell Module on Windows 10
VCP Muthukrishna
 
How to Fix Duplicate Packages in YUM on CentOS 7
VCP Muthukrishna
 

Similar to Windows PowerShell Basics – How To Create powershell for loop (20)

PDF
Powershell notes
Carlos Amorim
 
PPTX
Power shell training
David Brabant
 
PPTX
Get-Help: An intro to PowerShell and how to Use it for Evil
jaredhaight
 
PDF
Learn Powershell Scripting Tutorial Full Course 1dollarcart.com.pdf
ClapperboardCinemaPV
 
PPT
NIIT ISAS Q5 Report - Windows PowerShell
Phan Hien
 
PDF
PowerShell_LangRef_v3 (1).pdf
outcast96
 
PPTX
Getting Started With PowerShell Scripting
Ravikanth Chaganti
 
PDF
One man loves powershell once he failed
Kazuhiro Matsushima
 
PDF
Bash vs. vista_power_shell
Thomas Lee
 
PPTX
PowerShell 101
Thomas Lee
 
PPT
PowerShell Core Skills (TechMentor Fall 2011)
Concentrated Technology
 
PPTX
Scripting ppt
anamichintu
 
PPTX
PowerShell Zero To Hero Workshop!
Daisy Stevens
 
PDF
429 e8d01
vipinck2008
 
PPTX
Powershell Demo Presentation
Avik Bhattacharyya
 
PPTX
Holy PowerShell, BATman! - dogfood edition
Dave Diehl
 
PPTX
Unix Shell Programming subject shell scripting ppt
Radhika Ajadka
 
PPTX
Scripting ppt
anamichintu
 
PPTX
Powershell For Developers
Ido Flatow
 
PPTX
PowerShell for Penetration Testers
Nikhil Mittal
 
Powershell notes
Carlos Amorim
 
Power shell training
David Brabant
 
Get-Help: An intro to PowerShell and how to Use it for Evil
jaredhaight
 
Learn Powershell Scripting Tutorial Full Course 1dollarcart.com.pdf
ClapperboardCinemaPV
 
NIIT ISAS Q5 Report - Windows PowerShell
Phan Hien
 
PowerShell_LangRef_v3 (1).pdf
outcast96
 
Getting Started With PowerShell Scripting
Ravikanth Chaganti
 
One man loves powershell once he failed
Kazuhiro Matsushima
 
Bash vs. vista_power_shell
Thomas Lee
 
PowerShell 101
Thomas Lee
 
PowerShell Core Skills (TechMentor Fall 2011)
Concentrated Technology
 
Scripting ppt
anamichintu
 
PowerShell Zero To Hero Workshop!
Daisy Stevens
 
429 e8d01
vipinck2008
 
Powershell Demo Presentation
Avik Bhattacharyya
 
Holy PowerShell, BATman! - dogfood edition
Dave Diehl
 
Unix Shell Programming subject shell scripting ppt
Radhika Ajadka
 
Scripting ppt
anamichintu
 
Powershell For Developers
Ido Flatow
 
PowerShell for Penetration Testers
Nikhil Mittal
 
Ad

More from VCP Muthukrishna (11)

PDF
How To Create PowerShell Function Mandatory Parameter and Optional Parameter
VCP Muthukrishna
 
PDF
How To Configure Nginx Load Balancer on CentOS 7
VCP Muthukrishna
 
PDF
Nginx bind() to 0.0.0.0:9080 failed
VCP Muthukrishna
 
PDF
How To Install and Configure Screen on CentOS 7
VCP Muthukrishna
 
PDF
How To Install and Configure Salt Master on Ubuntu
VCP Muthukrishna
 
PDF
How To Protect SSH Access with Fail2Ban on RHEL 7
VCP Muthukrishna
 
PDF
How To Configure SNMP Logging on RHEL 7
VCP Muthukrishna
 
PDF
How To Find Package Installation Date on RHEL 7
VCP Muthukrishna
 
PDF
How to Upgrade Openfire on CentOS 7
VCP Muthukrishna
 
PDF
How To Reset root Password on CentOS 7
VCP Muthukrishna
 
PDF
How To Install and Use ABRT CLI on RHEL 7
VCP Muthukrishna
 
How To Create PowerShell Function Mandatory Parameter and Optional Parameter
VCP Muthukrishna
 
How To Configure Nginx Load Balancer on CentOS 7
VCP Muthukrishna
 
Nginx bind() to 0.0.0.0:9080 failed
VCP Muthukrishna
 
How To Install and Configure Screen on CentOS 7
VCP Muthukrishna
 
How To Install and Configure Salt Master on Ubuntu
VCP Muthukrishna
 
How To Protect SSH Access with Fail2Ban on RHEL 7
VCP Muthukrishna
 
How To Configure SNMP Logging on RHEL 7
VCP Muthukrishna
 
How To Find Package Installation Date on RHEL 7
VCP Muthukrishna
 
How to Upgrade Openfire on CentOS 7
VCP Muthukrishna
 
How To Reset root Password on CentOS 7
VCP Muthukrishna
 
How To Install and Use ABRT CLI on RHEL 7
VCP Muthukrishna
 
Ad

Recently uploaded (20)

PDF
Proactive Server and System Monitoring with FME: Using HTTP and System Caller...
Safe Software
 
PDF
99 Bottles of Trust on the Wall — Operational Principles for Trust in Cyber C...
treyka
 
PPTX
Paycifi - Programmable Trust_Breakfast_PPTXT
FinTech Belgium
 
PDF
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
PDF
How to Comply With Saudi Arabia’s National Cybersecurity Regulations.pdf
Bluechip Advanced Technologies
 
PPTX
The birth and death of Stars - earth and life science
rizellemarieastrolo
 
PDF
Enhancing Environmental Monitoring with Real-Time Data Integration: Leveragin...
Safe Software
 
PDF
Unlocking FME Flow’s Potential: Architecture Design for Modern Enterprises
Safe Software
 
PPSX
Usergroup - OutSystems Architecture.ppsx
Kurt Vandevelde
 
PPTX
Enabling the Digital Artisan – keynote at ICOCI 2025
Alan Dix
 
PDF
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
 
PDF
''Taming Explosive Growth: Building Resilience in a Hyper-Scaled Financial Pl...
Fwdays
 
PDF
The Future of Product Management in AI ERA.pdf
Alyona Owens
 
PDF
Java 25 and Beyond - A Roadmap of Innovations
Ana-Maria Mihalceanu
 
PPTX
Smarter Governance with AI: What Every Board Needs to Know
OnBoard
 
PDF
TrustArc Webinar - Navigating APAC Data Privacy Laws: Compliance & Challenges
TrustArc
 
PDF
Why aren't you using FME Flow's CPU Time?
Safe Software
 
PDF
“Scaling i.MX Applications Processors’ Native Edge AI with Discrete AI Accele...
Edge AI and Vision Alliance
 
PDF
Optimizing the trajectory of a wheel loader working in short loading cycles
Reno Filla
 
PDF
How to Visualize the ​Spatio-Temporal Data Using CesiumJS​
SANGHEE SHIN
 
Proactive Server and System Monitoring with FME: Using HTTP and System Caller...
Safe Software
 
99 Bottles of Trust on the Wall — Operational Principles for Trust in Cyber C...
treyka
 
Paycifi - Programmable Trust_Breakfast_PPTXT
FinTech Belgium
 
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
How to Comply With Saudi Arabia’s National Cybersecurity Regulations.pdf
Bluechip Advanced Technologies
 
The birth and death of Stars - earth and life science
rizellemarieastrolo
 
Enhancing Environmental Monitoring with Real-Time Data Integration: Leveragin...
Safe Software
 
Unlocking FME Flow’s Potential: Architecture Design for Modern Enterprises
Safe Software
 
Usergroup - OutSystems Architecture.ppsx
Kurt Vandevelde
 
Enabling the Digital Artisan – keynote at ICOCI 2025
Alan Dix
 
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
 
''Taming Explosive Growth: Building Resilience in a Hyper-Scaled Financial Pl...
Fwdays
 
The Future of Product Management in AI ERA.pdf
Alyona Owens
 
Java 25 and Beyond - A Roadmap of Innovations
Ana-Maria Mihalceanu
 
Smarter Governance with AI: What Every Board Needs to Know
OnBoard
 
TrustArc Webinar - Navigating APAC Data Privacy Laws: Compliance & Challenges
TrustArc
 
Why aren't you using FME Flow's CPU Time?
Safe Software
 
“Scaling i.MX Applications Processors’ Native Edge AI with Discrete AI Accele...
Edge AI and Vision Alliance
 
Optimizing the trajectory of a wheel loader working in short loading cycles
Reno Filla
 
How to Visualize the ​Spatio-Temporal Data Using CesiumJS​
SANGHEE SHIN
 

Windows PowerShell Basics – How To Create powershell for loop

  • 1. Windows PowerShell Basics – For Loop i | P a g e Table of Contents Overview.......................................................................................................................................................1 Applies To..................................................................................................................................................1 Pre-Requisites ...........................................................................................................................................1 PowerShell Script – Function ........................................................................................................................1 Code Snippet – For loop............................................................................................................................1 PowerShell Output – For Loop..............................................................................................................2
  • 2. Windows PowerShell Basics – For Loop 1 | P a g e Overview PowerShell functions are helpful in saving time when you have repetitive task(s) to be executed. In this guide we will demonstrate, as to how to construct a “for” loop with different incremental and decremented values. Applies To Tested on Windows 10, Windows 2008 R2 and Windows 2012. Pre-Requisites Launch PowerShell Command Console or PowerShell ISE. To run this script, Execution Policy should be set to either of these “AllSigned” or “RemoteSigned” or “Unrestricted”, you can get current execution policy by running the command; “Get-ExecutionPolicy”. Each Policy type and its purpose is shown in the below table. Policy Type Purpose Restricted No scripts can be run. Windows PowerShell can be used only in interactive mode. AllSigned Only scripts signed by a trusted publisher can be run. RemoteSigned Downloaded scripts must be signed by a trusted publisher before they can be run. Unrestricted No restrictions; all Windows PowerShell scripts can be run. PowerShell Script – Function This PowerShell script demonstrates the usage of for loop in a PowerShell. Code Snippet – For loop The code snippet is for demonstrating “for” loop usage, wherein incremental and decrementing values are demonstrated. Clear-Host Write-Host "`nIncrement by 1 for loop" -BackgroundColor Green -ForegroundColor White # # Increment by 1 # for($Iterator=0; $Iterator -le 5; $Iterator++) { Write-Host "Increment by 1 - Current Incremental Value " $Iterator [console]::Beep(65, 100) }
  • 3. Windows PowerShell Basics – For Loop 2 | P a g e # # Skip +2 or Increment by 2 # Start-Sleep -Seconds 2 Write-Host "`nIncrement by 2 for loop" -BackgroundColor Green -ForegroundColor White for($Iterator=0; $Iterator -le 10; $Iterator += 2) { Write-Host "Increment by 2 - Current Incremental Value" $Iterator [console]::Beep(37, 100) } # # Decrement by 1 # Start-Sleep -Seconds 2 Write-Host "`nDecrement by 1 for loop" -BackgroundColor Green -ForegroundColor White for($Iterator=0; $Iterator -ge -5; $Iterator--) { Write-Host "Decrement by 1 - Current Decremented Value" $Iterator [console]::Beep(37, 100) } # # Skip -2 or Decrement by 2 # Start-Sleep -Seconds 2 Write-Host "`n Skip -2 or Decrement by 2 for loop" -BackgroundColor Green -ForegroundColor White for($Iterator=0; $Iterator -ge -10; $Iterator -= 2) { Write-Host "Decrement by 2 - Current Decremented Value" $Iterator [console]::Beep(37, 100) } PowerShell Output – For Loop When script is executed; below output will be displayed.