Created
October 8, 2025 21:08
-
-
Save rbleattler/8fa1166a2c0381f433ef6f670e1c537e to your computer and use it in GitHub Desktop.
Invoke-Countdown
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Invoke-Countdown { | |
param( | |
[Parameter(HelpMessage = "The number of seconds to wait before sleeping", ParameterSetName = "Seconds")] | |
[int] $Seconds = 90, | |
[Parameter(HelpMessage = "The number of minutes to wait before sleeping", ParameterSetName = "Minutes")] | |
[int] $Minutes = 5, | |
[Parameter(HelpMessage = "The activity to display in the progress bar")] | |
[string] | |
$Activity = "Counting down..." | |
) | |
begin { | |
if ($PSCmdlet.ParameterSetName -eq "Minutes") { | |
$Seconds = $Minutes * 60 | |
} | |
} | |
process { | |
$originalSeconds = $Seconds | |
$totalSeconds = $originalSeconds | |
while ($totalSeconds -gt 0) { | |
Start-Sleep -Seconds 1 | |
$totalSeconds-- | |
$timeRemaining = "{0}:{1}" -f (($totalSeconds / 60) -ge 1 ? [System.Math]::Round(($totalSeconds / 60), 0, [MidpointRounding]::ToZero) : 0), ($totalSeconds % 60) | |
$percentComplete = 100 - (($totalSeconds / $originalSeconds)) * 100 | |
Write-Debug "Percent complete: $percentComplete" | |
$percentString = [Math]::Round($percentComplete, 0) | |
Write-Progress -Activity "$Activity ($percentString%)" -Status "Time remaining: $timeRemaining" -PercentComplete $([math]::Round($percentComplete, 0, [MidpointRounding]::ToEven)) -SecondsRemaining $totalSeconds | |
} | |
} | |
end { | |
Write-Progress -Activity $Activity -Completed | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment