A nifty powershell timer module.
I use Powershell for setting up renders a lot. And often I want to give something time to render, then compress it, send it to the internets, shut down the workstation, whatever. I used to use the Start-Sleep commandlet (Sleep to its friends), but because I like complicating things here’s a nifty replacement with bells and whistles.
This module gives you a countdown timer. You specify how long you want it to run, and it ticks away, counting the seconds down, it’s even got an old-school spinner. That’s all it does, but I think it does it well.
To run specify any combination of -Days, -Hours, -Minutes, or -Seconds you want it to run. You can also specify -TickLength which determines how often it updates, but doesn’t affect the length that it waits.
SYNTAX Start-CountdownTimer [[-Days] <int>] [[-Hours] <int>] [[-Minutes] <int>] [[-Seconds] <int>] [[-tickLength] <int>]
Installing:
Using Powershell (recommended)
Install-Module -Name start-countdowntimer
fork it from github
dcas
Hello,
I took some liberty with your code and changed it to as follows. Why did care about CursorPosition? I assume because you wanted your function to work in different shells ?
function Start-CountdownTimer {
[alias(“Start-CDT”)]
param (
[parameter(Mandatory = $False)][int]$Days,
[parameter(Mandatory = $False)][int]$Hours,
[parameter(Mandatory = $False)][int]$Minutes,
[parameter(Mandatory = $False)][int]$Seconds,
[parameter(Mandatory = $False)][int]$TickLength = 1,
[parameter(Mandatory = $False)][switch]$NoTitle
)
if ((!$NoTitle) -and (($days) -or ($Hours) -or ($Minutes) -or ($Seconds))){ Write-Host 'Timer starting now' -ForegroundColor Green } If ((!$days) -and (!$Hours) -and (!$Minutes) -and (!$Seconds) -and ($Seconds -ne 15)) { if (!$NoTitle) { Write-Host 'Default 15 second timer starting now' -ForegroundColor Green } $Seconds = 15 } iF (!$NoTitle) { Write-Host "Press any key to end timer" -ForegroundColor Cyan } $VarTime = New-TimeSpan -Days $Days -Hours $Hours -Minutes $Minutes -Seconds $Seconds $spinner = @('|', '/', '-', '\') $spinnerPos = 0 $CalcTime = ( get-date) + $VarTime $remain = ($Calctime - (get-date)) while ($remain.TotalSeconds -gt 0) { write-host " " -NoNewline Write-Host (" {0} " -f $spinner[$spinnerPos % 4]) -BackgroundColor Red -ForegroundColor Black -NoNewline If ($Seconds) { write-host (" {3:d2}s " -f $remain.Days, $remain.Hours, $remain.Minutes, $remain.Seconds) -ForegroundColor Yellow -NoNewline } If ($Minutes) { write-host (" {2:d2}m {3:d2}s " -f $remain.Days, $remain.Hours, $remain.Minutes, $remain.Seconds) -ForegroundColor Blue -NoNewline } If ($Hours){ write-host (" {1:d2}h {2:d2}m {3:d2}s " -f $remain.Days, $remain.Hours, $remain.Minutes, $remain.Seconds) -ForegroundColor Blue -NoNewline } If ($Days){ write-host (" {0}D {1:d2}h {2:d2}m {3:d2}s " -f $remain.Days, $remain.Hours, $remain.Minutes, $remain.Seconds) -ForegroundColor Blue -NoNewline } Write-Host $spinnerPos += 1 Start-Sleep -seconds $TickLength $remain = ($Calctime - (get-date)) if ([Console]::KeyAvailable) { [Console]::ReadKey($true) | Out-Null break } }
}