Stopping PowerShell window with visible countdown from constantly scrolling its content to bottom
Posted: Sat Aug 05, 2023 12:48 pm
I have this PS1 script
It halts at a visually pleasing countdown and after it ends it proceeds with execution of its remaining part
But the problem with it is that each next shown digit makes the PowerShell window scroll down its content to its bottom - thus I can see only for less than second what is above this active counter. I mean: I can constantly scroll up manually but nevertheless it always goes back to the bottom to show a new digit. I could of course use a silent countdown like this
but by doing so I would not be informed of how much time has left till execution
So is there a way to eat a cake and have it - i.e. to see a dynamic countdown at the bottom of PS window but also at any time to be able to scroll up within it and not have focus thrown down every second?
Code: Select all
# Text to be read by user:
Write-Host "Line of text at the very top"
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host "Line of text in the middle"
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host "Last line of text"
Write-Host "Executing in:"
Write-Host
# Countdown:
$topRow = [System.Console]::CursorTop
3..0 | ForEach-Object {
[System.Console]::SetCursorPosition(0, $topRow)
if ($_ -eq 3) { # Showcase of the first digit / second
Write-Host
Write-Host
Write-Host " Executing deletion in"
Write-Host
Write-Host " ► $_ ◄"
Write-Host " "
Write-Host " seconds"
Write-Host
} else { # Showcase of the remaining digits / seconds
Write-Host
Write-Host
Write-Host " Executing deletion in"
Write-Host
Write-Host " ► $_ ◄"
Write-Host " "
Write-Host " seconds"
Write-Host
}
Start-Sleep -Seconds 1
Write-Host " `b"
}
# Here will be the main script
# Post execution message
Write-Host
Write-Host "Script has been executed"
Read-Host
But the problem with it is that each next shown digit makes the PowerShell window scroll down its content to its bottom - thus I can see only for less than second what is above this active counter. I mean: I can constantly scroll up manually but nevertheless it always goes back to the bottom to show a new digit. I could of course use a silent countdown like this
Code: Select all
$CountdownSeconds = 3
Write-Host "Executing in:"
Write-Host $CountdownSeconds
Write-Host "seconds"
Start-Sleep -Seconds $CountdownSeconds
So is there a way to eat a cake and have it - i.e. to see a dynamic countdown at the bottom of PS window but also at any time to be able to scroll up within it and not have focus thrown down every second?