Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Powershell sleep function, with progress bar.
function Sleep-Progress($seconds)
Function to Start-Sleep with a progress bar
Runs the 'Start-Sleep' command using the with a progress bar. Time is passed to the function in seconds as an argument.
# Updated from original to include the 'Wait time' in minutes and seconds
Sleep-Progress 300
$doneDT = (Get-Date).AddSeconds($seconds)
while($doneDT -gt (Get-Date)) {
$secondsLeft = $doneDT.Subtract((Get-Date)).TotalSeconds
$percent = ($seconds - $secondsLeft) / $seconds * 100
Write-Progress -Activity "Waiting $($([timespan]::fromseconds($seconds)).ToString("mm\:ss")) minutes ..." -Status "Waiting..." -SecondsRemaining $secondsLeft -PercentComplete $percent
Write-Progress -Activity "Waiting $($([timespan]::fromseconds($seconds)).ToString("mm\:ss")) minutes ..." -Status "Waiting..." -SecondsRemaining 0 -Completed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment