Soem time ago I wrote a script for a customer which does some things in the background that may take quite some time.
So from their perspective the script would ‘hang’.
To avoid this experience I’ve written a small function to provide a somewhat better experience by showing a progress bar.
1 2 3 4 5 6 7 8 9 10 11 12 |
function TakeABreak { param ( [parameter(mandatory=$true)][int]$Seconds ) $Step = 100 / $seconds $i = 0 while ($i -le 100) { Write-Progress -Activity "Take a break..." -PercentComplete $i -CurrentOperation "$i% complete" -Status "Please wait." Start-Sleep -Seconds 1 $i = $i + $Step } } |
Note that the purpose of this function is to be functional, not to be accurate. The script takes some milliseconds to execute, which are not deducted from the pause.
In my case I had the advantage of calculating the time for script to execute, which I’ve taken as input for the function above. 🙂