Stop visible countdown from scrolling down content in PowerShell window

Microsoft Windows
Post Reply
SS##66
Posts: 28
Joined: 2023-Jan-24, 4:51 pm

Stop visible countdown from scrolling down content in PowerShell window

Post by SS##66 »

I have this A-OK working script

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))
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

Code: Select all

$CountdownSeconds = 10
Write-Host "Executing in:"
Write-Host $CountdownSeconds
Write-Host "seconds"
Start-Sleep -Seconds $CountdownSeconds
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?
Last edited by SS##66 on 2023-Sep-12, 1:54 pm, edited 1 time in total.
SS##66
Posts: 28
Joined: 2023-Jan-24, 4:51 pm

Re: Stop visible countdown from scrolling down content in PowerShell window

Post by SS##66 »

SS##66 wrote: 2023-Aug-03, 9:47 am [...]
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?
Any ideas about how can this be done?
SS##66
Posts: 28
Joined: 2023-Jan-24, 4:51 pm

Re: Stop visible countdown from scrolling down content in PowerShell window

Post by SS##66 »

It seems that currently in the Windows Console [conhost.exe] it is impossible to see digits of a countdown in progress at the bottom of window while also retaining ability to scroll it up an not have the view being thrown back down after passing of yet another second. The new Windows Terminal [windowsterminal.exe] however is supposedly to not snap to the bottom on output if user scrolls up- as it only snaps to the bottom on output if user is already at the bottom of window when it occurs

And so, as Windows Terminal by default replaces the old Windows Terminal since Windows 11 22H2, but installing it on older systems has many prerequisites [https://github.com/Microsoft/Terminal#prerequisites] - thus it is easier to just wait out this issue

[More info: https://github.com/microsoft/terminal/issues/15962]
Post Reply