r/PowerShell • u/icebreaker374 • Sep 17 '26
Question Write-Progress
Looking for opinions...
When using Write-Progress inside of for/Foreach loops and using a counter stored in a variable defined outside the loop, do you start the counter at 0 or 1, and why?
13
Upvotes
View all comments
Show parent comment
1
u/FeleaseRpseineEiles Sep 17 '26
Yea, I understand the nuance. The way I see it, we're inside the first step's closure therefore we've progressed more than zero, we are on the first step and show that we're progressing. We aren't yet complete and on the last item I'd want to show that we're on the last step 100% then -Completed
the percent would need some work for more than 100 items too.
$step = $i + 1$percent = [Math]::Min(100, [int](($step / $total) * 100))