I’ve been using Write-Progress a lot more, not just because Don Jones told me to stop using Write-Host but because it’s such an elegant approach to writing output to the shell. While getting familiar with it, I’ve created a simple little script that simply counts from 1 to 10 while outputting some text and the percent complete. It’s nothing fancy, but might be good for people trying to understand how Write-Progress works and how to get dynamic percentages using the -PercentComplete parameter.
The script itself is very simple, i just set $i to 0 and $iMax to 10 and then use a do/while loop to write out the progress while $i is less than $iMax. I could’ve done this a few different ways, but this one made sense to me.
Here is the script:
$i=0 $iMax = 10 do { $i++ Write-Progress -Activity "Doing something awesome" -PercentComplete ($i/$iMax*100) -Status $i Start-Sleep -Seconds 1 } while ($i -lt $iMax)
And here’s what it looks like while running, in this screenshot it’s 20% complete: