Loop Counter

noun

say it: “LOOP KOWN-ter” /ˈlup ˈkaʊntər/

A variable that goes up by one, or by an amount, each time a loop goes round, to count items or add up a total.

What is a loop counter in PowerShell?

A loop counter is a variable you start at 0 before a loop and add to inside it, like a tally on scrap paper. $count++ adds one, and $total += $minutes keeps a running total. Set inside the loop, it would go back to 0 on every pass.

$count = 0
$total = 0
foreach ($song in Import-Csv playlist.csv) {
    $count++
    $total += [int]$song.Minutes
}
"$count songs, $total minutes"
# 6 songs, 29 minutes

In a sentence

  • “The rename script keeps a loop counter and ends with Files renamed: 30.”
  • “Sam started his loop counter at 0 before the loop, not inside it.”

Level: Apprentice · More PowerShell terms at this level

Updated

In other subjects