Powershell loops (for, foreach, and foreach-object) when you there is a need to execute the sequence code for several times to complete a task.
Powershell For loop syntax and examples:
for (<Init>; <Condition>; <Repeat>)
{ <your statement list>}
# Example1 for($i = 0; $i -le 10; $i++) { "$i" } # Example2 for (($i = 0), ($r = 0); $i -lt 10 -and $r -lt 10; $i++,$r++) { "`$i:$i" "`$r:$r" }
Powershell Foreach loop syntax and examples:
The foreach loop is used for executing code against a series of array objects stored in a variable and during each cycle, the ForEach operator executes a {Statement Block} on each individual item.
Syntax
ForEach (oneitem In collection) {ScriptBlock}
Oneitem: it’s one of the element or object of collection variable.
Collection: a series of array objects.
ScriptBlock: A block of the script runs against each object.
# example1 $serverlist = @ ("server1" ," server2", "server3") Foreach ($server in $serverlist) { Write-host "Servername is" $server Invoke-command -computername $server -scriptblock {Get-service -name raj} } # example2 foreach ($num in 1,2,3,4,5) { Write-host " my number is" $num }
Powershell Foreach-object loop syntax and examples:
This foreach-object uses pipeline to executes the task, it takes input objects from the pipeline and runs against each item in the collection.
Syntax
ForEach-Object [-process] ScriptBlock[] [-inputObject psobject]
#Example1: $names = @("raj","shekar","prava","aryan") $names | ForEach-Object { "Hello, my name is $_!" } #Example2: get-childitem -path "C:\windows" | foreach-object -process { $_.length / 1024 }
Powershell While loop syntax and examples:
This while loop runs a command block based on the result of a conditional test given.
Syntax:
while (condition) {command_block}
# count to 20 $count = 0 While ($count -ne 20) { $count ++ Write-host $count }
Powershell DO loop syntax and examples:
Do-loops is used when you want to run a codeblock at least once. A Do-loop evaluates the condition
after executing the code block, unlike a while-loop which does it before executing the code block.
Syntax:
Do
{
code_block
}
while (condition)
I’m learning PowerShell today!
Loop until the condition is true, in other words, loop while the condition is false:
# Examples:
$i = 0 Do { $i++ "item is $i" } while ($i -ne 3) $i= 0 Do { $i++ "item is $i" } until ($i -eq 3)
Powershell Switch loop statement.
This switch statement provides a variable and a list of possible values, when the values match the given variable inside statement then its script block will be executed.
Example:
switch ( $today ) { 0 { $myday = 'Sunday' } 1 { $myday = 'Monday' } 2 { $myday = 'Tuesday' } 3 { $myday = 'Wednesday' } 4 { $myday = 'Thursday' } 5 { $myday = 'Friday' } 6 { $myday = 'Saturday' } } $today= Read-Host "Choose the number" Write-Host "Your day is" $myday
Thank you for reading this article, if you have any questions please let us know.
Thank you for visiting my site, please make sure you have tested this script in our lower environment before you run in production.
Leave a Reply Cancel reply