Clear-Host does not clear the previous content (Powershell) #4361

Closed
opened 2026-01-30 23:45:08 +00:00 by claunia · 12 comments
Owner

Originally created by @cdmihai on GitHub (Oct 9, 2019).

Originally assigned to: @zadjii-msft on GitHub.

Environment

Windows build number: Microsoft Windows NT 10.0.18362.0
Windows Terminal version (if applicable): 0.5.2762.0

Steps to reproduce

# open Terminal with powershell
# open git repo with more than 100 commits
git log -100 # to fill in the entire screen with text
Clear-Host
# scroll up

Expected behavior

Cannot scroll up, Clear-Host cleared the terminal, all previous output is gone.

Actual behavior

Scrolling up shows previous output. This repros with both Powershel 6.2.3 and 5.2.1.
This does not happen with the powershell terminal, which correctly removes all previous output.

Originally created by @cdmihai on GitHub (Oct 9, 2019). Originally assigned to: @zadjii-msft on GitHub. # Environment ```none Windows build number: Microsoft Windows NT 10.0.18362.0 Windows Terminal version (if applicable): 0.5.2762.0 ``` # Steps to reproduce ``` # open Terminal with powershell # open git repo with more than 100 commits git log -100 # to fill in the entire screen with text Clear-Host # scroll up ``` # Expected behavior Cannot scroll up, `Clear-Host` cleared the terminal, all previous output is gone. # Actual behavior Scrolling up shows previous output. This repros with both Powershel 6.2.3 and 5.2.1. This does not happen with the powershell terminal, which correctly removes all previous output.
Author
Owner

@j4james commented on GitHub (Oct 9, 2019):

I think this is somewhat related to #2715. Not necessarily a duplicate, but I suspect that fixing this issue might also fix #2715.

@j4james commented on GitHub (Oct 9, 2019): I think this is somewhat related to #2715. Not necessarily a duplicate, but I suspect that fixing this issue might also fix #2715.
Author
Owner

@ezhikov commented on GitHub (Oct 15, 2019):

Same thing with WSL, where clear does not cleaning scrollback.

@ezhikov commented on GitHub (Oct 15, 2019): Same thing with WSL, where `clear` does not cleaning scrollback.
Author
Owner

@DHowett-MSFT commented on GitHub (Oct 23, 2019):

Just checked: this will almost certainly be fixed by /dup #2715. If PowerShell is clearing the screen in another way (which it might be doing), we'll have to evaluate then. PowerShell Core, however, can always be updated to take advantage of CSI 3 J

@DHowett-MSFT commented on GitHub (Oct 23, 2019): Just checked: this _will_ almost certainly be fixed by /dup #2715. If PowerShell is clearing the screen in another way (which it might be doing), we'll have to evaluate then. PowerShell Core, however, can always be updated to take advantage of CSI 3 J
Author
Owner

@ghost commented on GitHub (Oct 23, 2019):

Hi! We've identified this issue as a duplicate of another one that already exists on this Issue Tracker. This specific instance is being closed in favor of tracking the concern over on the referenced thread. Thanks for your report!

@ghost commented on GitHub (Oct 23, 2019): Hi! We've identified this issue as a duplicate of another one that already exists on this Issue Tracker. This specific instance is being closed in favor of tracking the concern over on the referenced thread. Thanks for your report!
Author
Owner

@miniksa commented on GitHub (Feb 3, 2020):

We're going to need to look at this again. We're going to pass through CSI 3 J but that's not going to solve folks who are rotating the entire backing buffer with the API (or something else) to clear it out.

We probably need to consider and engage on fixing those.

@miniksa commented on GitHub (Feb 3, 2020): We're going to need to look at this again. We're going to pass through `CSI 3 J` but that's not going to solve folks who are rotating the entire backing buffer with the API (or something else) to clear it out. We probably need to consider and engage on fixing those.
Author
Owner

@DHowett-MSFT commented on GitHub (Feb 7, 2020):

I'm pulling the triage tag and putting this in v1.0: this is a complaint we receive so often that we'd be remiss to ship a final product without giving it an investigation.

@DHowett-MSFT commented on GitHub (Feb 7, 2020): I'm pulling the triage tag and putting this in v1.0: this is a complaint we receive so often that we'd be remiss to ship a final product without giving it an investigation.
Author
Owner

@DHowett-MSFT commented on GitHub (Feb 10, 2020):

It looks like both CMD and PowerShell...

  1. set the cursor to 0,0
  2. fill the entire console buffer with spaces

PowerShell further..

  1. stomps the attributes on the entire buffer with the (narrowed) buffer attributes

This might(?) be detectable.

@DHowett-MSFT commented on GitHub (Feb 10, 2020): It looks like both CMD and PowerShell... 1. set the cursor to 0,0 2. fill the entire console buffer with spaces PowerShell further.. 3. stomps the attributes on the entire buffer with the (narrowed) buffer attributes This _might_(?) be detectable.
Author
Owner

@DHowett-MSFT commented on GitHub (Feb 10, 2020):

Discussion from triage room: we can probably quirk it for cmd and powershell (powershell and pwsh.exe).

@DHowett-MSFT commented on GitHub (Feb 10, 2020): Discussion from triage room: we can probably quirk it for cmd and powershell (powershell and pwsh.exe).
Author
Owner

@PrzemyslawKlys commented on GitHub (Feb 18, 2020):

This issue was very common in VsCode/PowerShell. But they managed to fix it. Here's a PR: https://github.com/PowerShell/vscode-powershell/pull/2316

Maybe it can be easily ported.

@PrzemyslawKlys commented on GitHub (Feb 18, 2020): This issue was very common in VsCode/PowerShell. But they managed to fix it. Here's a PR: https://github.com/PowerShell/vscode-powershell/pull/2316 Maybe it can be easily ported.
Author
Owner

@dragonwolf83 commented on GitHub (Feb 19, 2020):

No, they did a workaround that is specific to that project and VS Code. It can't even be ported to other PowerShell Editor Service Clients. They run a custom console so they changed the clear command to run a VS Code command. The regular PowerShell and CMD consoles do not clear in VS Code.

@dragonwolf83 commented on GitHub (Feb 19, 2020): No, they did a workaround that is specific to that project and VS Code. It can't even be ported to other PowerShell Editor Service Clients. They run a custom console so they changed the clear command to run a VS Code command. The regular PowerShell and CMD consoles do not clear in VS Code.
Author
Owner

@zadjii-msft commented on GitHub (Apr 28, 2020):

Self notes from what Dustin commented months ago during triage:

PowerShell

Powershell clears the screen with:
8dae4915d3/src/System.Management.Automation/engine/InitialSessionState.cs (L4063-L4075)

            if (Platform.IsWindows)
            {
                // use $RawUI so this works over remoting where there isn't a physical console
                return @"
$RawUI = $Host.UI.RawUI
$RawUI.CursorPosition = @{X=0;Y=0}
$RawUI.SetBufferContents(
    @{Top = -1; Bottom = -1; Right = -1; Left = -1},
    @{Character = ' '; ForegroundColor = $rawui.ForegroundColor; BackgroundColor = $rawui.BackgroundColor})
# .Link
# https://go.microsoft.com/fwlink/?LinkID=225747
# .ExternalHelp System.Management.Automation.dll-help.xml
";
            }

Which then calls to:
b1e998046e/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostRawUserInterface.cs (L957-L971)

            if (region.Left == -1 && region.Right == -1 && region.Top == -1 && region.Bottom == -1)
            {
                if (bufferWidth % 2 == 1 &&
                    ConsoleControl.IsCJKOutputCodePage(out codePage) &&
                    LengthInBufferCells(fill.Character) == 2)
                {
                    throw PSTraceSource.NewArgumentException("fill");
                }

                int cells = bufferWidth * bufferHeight;

                ConsoleControl.FillConsoleOutputCharacter(handle, fill.Character, cells, origin);
                ConsoleControl.FillConsoleOutputAttribute(handle, attribute, cells, origin);
                return;
            }

CMD

CMD on the other hand calls

	ScrollConsoleScreenBuffer( handle, { 0, 0, bufferWidth, bufferHeight }, nullptr,  { 0, -bufferHeight }, {space with the current attributes});
    SetConsoleCursorPosition( handle, {0, 0} );

(more or less)

@zadjii-msft commented on GitHub (Apr 28, 2020): Self notes from what Dustin commented months ago during triage: ## PowerShell Powershell clears the screen with: https://github.com/iSazonov/PowerShell/blob/8dae4915d3072cbac54a9312cf524ff7a73c977a/src/System.Management.Automation/engine/InitialSessionState.cs#L4063-L4075 ```c# if (Platform.IsWindows) { // use $RawUI so this works over remoting where there isn't a physical console return @" $RawUI = $Host.UI.RawUI $RawUI.CursorPosition = @{X=0;Y=0} $RawUI.SetBufferContents( @{Top = -1; Bottom = -1; Right = -1; Left = -1}, @{Character = ' '; ForegroundColor = $rawui.ForegroundColor; BackgroundColor = $rawui.BackgroundColor}) # .Link # https://go.microsoft.com/fwlink/?LinkID=225747 # .ExternalHelp System.Management.Automation.dll-help.xml "; } ``` Which then calls to: https://github.com/PowerShell/PowerShell/blob/b1e998046e12ebe5da9dee479f20d479aa2256d7/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostRawUserInterface.cs#L957-L971 ```c# if (region.Left == -1 && region.Right == -1 && region.Top == -1 && region.Bottom == -1) { if (bufferWidth % 2 == 1 && ConsoleControl.IsCJKOutputCodePage(out codePage) && LengthInBufferCells(fill.Character) == 2) { throw PSTraceSource.NewArgumentException("fill"); } int cells = bufferWidth * bufferHeight; ConsoleControl.FillConsoleOutputCharacter(handle, fill.Character, cells, origin); ConsoleControl.FillConsoleOutputAttribute(handle, attribute, cells, origin); return; } ``` ## CMD CMD on the other hand calls ```c++ ScrollConsoleScreenBuffer( handle, { 0, 0, bufferWidth, bufferHeight }, nullptr, { 0, -bufferHeight }, {space with the current attributes}); SetConsoleCursorPosition( handle, {0, 0} ); ``` (more or less)
Author
Owner

@ghost commented on GitHub (May 5, 2020):

:tada:This issue was addressed in #5627, which has now been successfully released as Windows Terminal Release Candidate v0.11.1251.0 (1.0rc1).🎉

Handy links:

@ghost commented on GitHub (May 5, 2020): :tada:This issue was addressed in #5627, which has now been successfully released as `Windows Terminal Release Candidate v0.11.1251.0 (1.0rc1)`.:tada: Handy links: * [Release Notes](https://github.com/microsoft/terminal/releases/tag/v0.11.1251.0 (1.0rc1)) * [Store Download](https://www.microsoft.com/store/apps/9n0dx20hk701?cid=storebadge&ocid=badge)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#4361