Contents

PowerShell For loop

Contents

For loop - do you actually need it, since there’s foreach loop? Turns out, yes - there are situations where for loop comes very handy.

In this video I’ll show you the basic syntax of the for loop, going through nested for loops to end up with real case scenario from Azure Application Insights.

  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
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# Link to the video: https://youtu.be/YQnBVn-9SN0

# Documentation:
# https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_for?view=powershell-7.1

# Basic syntax:

# for (Initial Value/statement; Condition; Repeat) { Run my code }

# Initial value: Set this is up before starting the loop 
# Condition: For loops ends, when condition evaluates to false; or it keep running as lon as condition is true
# Repeat: Do this after every loop

for ($MyVariable = 0; $MyVariable -lt 10; $MyVariable = $MyVariable + 1) {
    '$MyVariable is {0}' -f $MyVariable
    Start-Sleep -Seconds 2
}

for ($MyVariable = 0; $MyVariable -lt 99; $MyVariable++) {
    '$MyVariable is {0}' -f $MyVariable
}

# We can also decrease it
for ($MyVariable = 10; $MyVariable -gt 5; $MyVariable = $MyVariable - 1) {
    '$MyVariable is {0}' -f $MyVariable
}

# or specify variable outside if
$outside = 7
for (; $outside -lt 15; $outside++) {
    '$Outside is {0}' -f $outside
}

#Looping through array
$pets = @("Cat", "Dog", "Fish", "Turtle")
$pets.Count

$pets[2]

"My pets in order:"
for ($i = 0; $i -lt $pets.Count; $i++) {
    $pets[$i]
}

# How about I'd like to add numbers
""
"My pets in order:"
""
for ($i = 0; $i -lt $pets.Count; $i++) {
    $Number = $i + 1
    "  {0}. {1}" -f $Number, $pets[$i]
}

# For also works with strings
for ($text = ''; $text.Length -lt 10; $text += '@') {
    $text
}

# Nested for
$row = 1..10
$column = 1..10

for ($i = 0; $i -lt $row.Count; $i++) {
    for ( $j = 0; $j -lt $column.Count; $j++) {

        $r = $row[$i]
        $c = $column[$j]
        $result = $r * $c

        $t = "{0} * {1} = {2}" -f $r, $c, $result
        Write-Host $t
    }
}
     
# It never ends... We need to break with CTRL - C
for ( $i = 0 ; ; $i++ ) {
    "Loop number: $I"
}

# Or we can break when certain condition is met 
for ( $i = 0 ; ; $i++ ) {
    "Loop number: $I"
    if ($i -eq 1000) {
        "Breaking out"
        break
    }
}

# A real case scenario with Azure Application Insigts

$Headers = @("First Name", "Email", "Phone Number")

$Rows = @(
    @("Kamil", "kamil@kamil.mail", "111-111-111"),
    @("John", "john@john.mail", "222-222-222"),
    @("Abigail", "abigail@abigail.mail", "333-333-333")
)

# List is like an array, however it allows to add new entries to it
$result = New-Object System.Collections.Generic.List[System.Object]

# We work with one record at the time
foreach ($row in $Rows) {

    # We need a blank PowerShell object. Notice we don't specify any properties up front
    $record = [PSCustomObject]@{}

    # Now we are going to iterate through each property in the row
    for ($i = 0; $i -lt $Headers.Count; $i++) {

        # Since we are working with PowerShell object, we can add members
        # Notice we use index for both name and value
        $record | Add-Member -MemberType NoteProperty -Name $Headers[$i] -Value $row[$i]
    }
    # We add completed object object to the list
    $result.Add($record)
}
#Display list with all records
$result