Stopping PowerShell window with visible countdown from constantly scrolling its content to bottom

Off-topic posts of interest to the "Everything" community.
Post Reply
Thy Grand Voidinesss
Posts: 687
Joined: Wed Jun 01, 2022 5:01 pm

Stopping PowerShell window with visible countdown from constantly scrolling its content to bottom

Post by Thy Grand Voidinesss »

I have this PS1 script

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

Code: Select all

$CountdownSeconds = 3
Write-Host "Executing in:"
Write-Host $CountdownSeconds
Write-Host "seconds"
Start-Sleep -Seconds $CountdownSeconds
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?
Thy Grand Voidinesss
Posts: 687
Joined: Wed Jun 01, 2022 5:01 pm

Re: Stopping PowerShell window with visible countdown from constantly scrolling its content to bottom

Post by Thy Grand Voidinesss »

Thy Grand Voidinesss wrote: Sat Aug 05, 2023 12:48 pm [...]
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?
Does anyone have an idea how to do it?
Thy Grand Voidinesss
Posts: 687
Joined: Wed Jun 01, 2022 5:01 pm

Re: Stopping PowerShell window with visible countdown from constantly scrolling its content to bottom

Post by Thy Grand Voidinesss »

It seems that currently in the Windows Console [conhost.exe] it is plain 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 current second. The new Windows Terminal [windowsterminal.exe] however is supposedly able to not snap to the bottom on output if a 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 some prerequisites [https://github.com/Microsoft/Terminal#prerequisites] - thus for me it is easier to just wait out this issue [as I use Windows 10 for now]

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