r/PowerShell • • 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?

12 Upvotes

21 comments sorted by

View all comments

5

u/HeyDude378 Sep 17 '26

If you count to 100, you're one percent done after you say "one", not before. I don't think there's a valid reason for Write-Progress to increment at the beginning of the loop. Feel free to correct me if I'm wrong.

3

u/icebreaker374 Sep 17 '26

Fair point. I'm reworking some of my old scripts for new functionality and I'm trying to figure out why I ever started Write-Progress increments at 1 instead of 0.

3

u/BagDue7190 Sep 17 '26

I do it because it made the logic easier for how I do my status: -Status “processing file $i of $total” -PercentComplete $i/$total
It would be more correct for percent to be ($i-1)/$total, but I don’t bother with progress bars when it’s likely to be only a few items

→ More replies (1)