-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_Loops-For.ps1
More file actions
46 lines (32 loc) · 1.23 KB
/
03_Loops-For.ps1
File metadata and controls
46 lines (32 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# The For statement (also known as a For loop) is a language construct you can use to create a loop
# that runs commands in a command block while a specified condition evaluates to $true.
# Basic Syntax:
# ----------------------------------------------------------------------------------------------------
# for (<Initial iterator value>; <Condition>; <Repeat>)
# {
# <Statement list>
# }
# the for statement
# ----------------------------------------------------------------------------------------------------
for ($i = 1; $i -lt 5; $i++) {
Write-Output "Sleeping for $i seconds"
Start-Sleep -Seconds $i
}
for ($i = 0; $i -le 20; $i += 2)
{
Write-Host "number $i"
}
# examples from: https://adamtheautomator.com/powershell-for-loop/
for ($x='' ;$x.length -le 30;$x=$x+'x'){
Write-Host $x
Start-Sleep -Milliseconds 20
}
$colors = @("Green","Cyan","Red","Magenta","Yellow","White")
for (($x=''),($fgcolor = $colors | Get-Random) ;$x.length -le 30;($x=$x+'x'),($fgcolor = $colors | Get-Random)){
Write-Host $x -ForegroundColor $fgcolor
Start-Sleep -Milliseconds 20
}
for ($seconds=10; $seconds -gt -1; $seconds--) {
Write-Host -NoNewLine ("`rseconds remaining: " + ("{0:d4}" -f $seconds))
Start-Sleep -Seconds 1
}