I've been looking to add a simple countdown timer to my script. I found this one that seemed to do the trick and modified it slightly to only display seconds since I don't need anything more than that.
When I run it, it will skip the 2nd second in the countdown. For example, if I ran Start-CountdownTimer -Seconds 10
, the output will be (this is split into separate lines for demo purposes since it'll be on the same line):
| Starting in 10s ...
/ Starting in 8s ...
- Starting in 7s ...
\ Starting in 6s ...
| Starting in 5s ...
/ Starting in 4s ...
- Starting in 3s ...
\ Starting in 2s ...
| Starting in 1s ...
/ Starting in 0s ...
Any ideas how I can fix this? This is the (slightly modified) code from the link above:
Function Start-CountdownTimer{
param (
<#[int]$Days = 0,
[int]$Hours = 0,
[int]$Minutes = 0,#>
[int]$Seconds = 0,
[int]$TickLength = 1
)
$t = New-TimeSpan <#-Days $Days -Hours $Hours -Minutes $Minutes#> -Seconds $Seconds
$origpos = $host.UI.RawUI.CursorPosition
$spinner =@('|', '/', '-', '\')
$spinnerPos = 0
$remain = $t
$d =( get-date) + $t
$remain = ($d - (get-date))
while ($remain.TotalSeconds -gt 0){
Write-Host (" {0} " -f $spinner[$spinnerPos%4]) -ForegroundColor White -NoNewline
write-Host (" Starting in {0:d1}s ..." -f $remain.Seconds)
$host.UI.RawUI.CursorPosition = $origpos
$spinnerPos += 1
Start-Sleep -seconds $TickLength
$remain = ($d - (get-date))
}
$host.UI.RawUI.CursorPosition = $origpos
}