Stop visible countdown from scrolling down content in PowerShell window
Posted: 2023-Aug-03, 9:47 am
I have this A-OK working script
It stops at a visually pleasing countdown and after it ends then proceeds with execution of the rest
But the problem with it is that every appearing digit makes the PS window scroll down its content to the very bottom - thus I can only see for less than second what is above this active counter. I can constantly scroll up manually but it always goes back to the bottom to show a new digit. I could of course replace that with a silent countdown like this
but then I would not be informed of how much time has left
So is there a way to eat a cake and have it - i.e. to really see a countdown at the bottom of window but also at any time to be able to scroll up not have my view thrown down every second?
Code: Select all
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
10..0 | ForEach-Object {
[System.Console]::SetCursorPosition(0, $topRow)
if ($_ -eq 10) {
Write-Host $_ -NoNewline
} else {
Write-Host " $_" -NoNewline
}
Start-Sleep -Seconds 1
Write-Host " `b" -NoNewline
}
# Here will be the main script
Write-Host
Write-Host "Script has been executed"
# Wait for specific user input (ESC, ENTER, or SPACE) before closing the window:
Write-Host "To close this window press either:
Enter
Space
Esc"
Write-Host
do {
$key = $Host.UI.RawUI.ReadKey('NoEcho, IncludeKeyDown').Character
} while ($key -notin @(' ', [char]13, [char]27))
But the problem with it is that every appearing digit makes the PS window scroll down its content to the very bottom - thus I can only see for less than second what is above this active counter. I can constantly scroll up manually but it always goes back to the bottom to show a new digit. I could of course replace that with a silent countdown like this
Code: Select all
$CountdownSeconds = 10
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 really see a countdown at the bottom of window but also at any time to be able to scroll up not have my view thrown down every second?