Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell Clear-Host alternative to preserve the buffer

I would like something like Clear-Host, but would like the scrollback.

So far, all I have is basically adding newlines until the screen is clear. Though when I do this, the cursor starts writing at the bottom of the page (as to be expect- though not desired).

I would hope to start writing at the top of the page (for menus and such- which is the natural thing to do)

This is the code to print the dynamic count of new lines:

do {Write-Host ""; $i++}
while ($i -ne $Host.UI.RawUI.WindowSize.Height)

I did see something about a Transcript module, but do not desire that.

Thanks in advance.

like image 850
krmarshall87 Avatar asked Sep 29 '22 09:09

krmarshall87


1 Answers

Here's a one-liner which gives exactly what you're looking for:

[System.Console]::SetWindowPosition(0,[System.Console]::CursorTop)

From here: http://tommymaynard.com/ql-clear-host-without-clearning-the-host/

The linked article also show how to use this in a Function for repeated use in a script.

like image 91
Mike Shepard Avatar answered Oct 05 '22 07:10

Mike Shepard