Skip to content

Enter-PSSession - Clear-Host from non-Windows client to Windows target fails #28115

Open

Description

Prerequisites

Steps to reproduce

When targeting a Windows host from a non-Windows host through Enter-PSSession calling Clear-Host fails. The Clear-Host implementation on a Windows target runs the following script:

# ${function:Clear-Host}

$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=2096480
# .ExternalHelp System.Management.Automation.dll-help.xml

Calling this in a remote PSSession will send the host method call back to the client which is fine with Windows client but a non-Windows client SetBufferContents for UNIX throws a NotImplementedException. The CursorPosition does still work but the contents are not cleared.

Expected behavior

The console is cleared and the cursor set back to position `0, 0`.

Actual behavior

The cursor position is set back to `0, 0`, `The method or operation is not implemented.` is displayed and original content is not cleared.

Error details

No explicit error for `Get-Error` as the exception isn't sent back to the remote session and just printed on the screen.

> The method or operation is not implemented.

Environment data

All Powershell 7.x versions.

Visuals

The below example shows what happens on a Linux client connecting to Windows.

Screen.Recording.2026-09-28.at.8.58.26.pm.mov

The same error occurs regardless of the transport protocol used.

Activity

  1. iSazonov commented on Sep 28, 2026

    @iSazonov
    Collaborator

    Since Windows XP/7 is not supported now, maybe a time to switch to VT Esc #21176 (comment)

  2. jborean93 commented on Sep 28, 2026

    @jborean93
    CollaboratorAuthor

    It would certainly be nice to have and it would align with how Clear-Host works when targeting a non-Windows host but I think at a minimum this should be fixed. Changing the mechanism to VT escape sequences would solve the problem if targeting Windows running the newer version of PowerShell 7.x with that change but when targeting older PowerShell versions, including 5.1, the client will need the fix I have in #28116 to support it. Unless MS is willing to backport the VT sequence change to 5.1 which I doubt would ever happen.

    Edit: Also see #28115 (comment) as to why the remote side should not emit the sequences directly.

  3. 237dmitry commented on Sep 28, 2026

    @237dmitry

    Native /usr/bin/clear outputs: `e[H`e[2J`e[3J. This is 4 times faster than Clear-Host:

    $ h 81, 95 
    
    Id Duration CommandLine
    -- -------- -----------
    81    0.008 cls
    95    0.002 "`e[H`e[2J`e[3J"

    Even considering that on Linux this function calls that very clear:

     $ gc function:Clear-Host                                                         
    
    [Console]::Write((
        & (Get-Command -CommandType Application clear | Select-Object -First 1).Definition
    ))
    # .Link
    # https://go.microsoft.com/fwlink/?LinkID=2096480
    # .ExternalHelp System.Management.Automation.dll-help.xml
    

    The best result:

    $ h -c 1                                  
    
     Id Duration CommandLine
     -- -------- -----------
    121    0.000 [console]::Write("`e[H`e[2J`e[3J")
  4. jborean93 commented on Sep 28, 2026

    @jborean93
    CollaboratorAuthor

    Ilya (@iSazonov) see #28118 as to why the VT sequences should not actually be emitted by the remote side. The remote should continue to send the PSRemoting specific host call method and the client/local interprets that to send the correct clear VT sequences where it knows more about the terminal being used.

    237dmitry that's fine but this issue is specifically around remote PSSessions and even the /usr/bin/clear and Clear-Host on a non-Windows target does not work in an entered PSSession as per #28118. See my point above as to why I think that the PSSession side needs to continue to use the host calls and the local side interprets and clears the console in whatever the local platform specific way is.

  5. 237dmitry commented on Sep 28, 2026

    @237dmitry

    /usr/bin/clear and Clear-Host on a non-Windows target does not work in an entered PSSession

    Why do not change Clear-Host function on Windows, I think esc-sequences have to work on every host (even on conhost.exe). But I cannot test it (no Windows at all).

    And on a Linux host, the pwsh has some limitations. As you wrote before.. For example:

    $ [console]::WindowWidth = 100       
    SetValueInvocationException: Exception setting "WindowWidth": "Operation is not supported on this platform."
    
  6. jborean93 commented on Sep 28, 2026

    @jborean93
    CollaboratorAuthor

    237dmitry this specific issue is in relation to Clear-Host and clear when used in a remote PSSession on Windows targets. As I stated in #28118 sending the raw VT sequences from the remote side for the local to interpret is not a good idea because the remote has no idea what terminal the local side is using and whether those VT sequences would work or not.

    I'm not saying the local/client side should be avoid these sequences, I'm saying the signal that remote PSSessions use to tell the client should stay this PSRP host method call so that the client and then interpret and use to clear the screen based on the terminfo it has. In fact that is exactly what Console.Clear does on *nix. We just need to ensure that the Console.Clear() call is done on the local side and not remote.

  7. 237dmitry commented on Sep 28, 2026

    @237dmitry

    I tested the SSH connection between two Linux systems, and the ESC sequences work without issues. As far as I recall, they work in Windows terminals as well.

  8. jborean93 commented on Sep 28, 2026

    @jborean93
    CollaboratorAuthor

    Remember this is for Enter-PSSession through PSRemoting and not ssh directly. PSRemoting does not share a tty but goes through a PowerShell specific protocol to exchange input and output. In any case using Console.Clear() will do what you want and is what I called in #28116 to fix this specific issue. The fix was to have the client do the Console.Clear() when it receives the specific PSRemoting host call that clear on the remote side for Windows sends.

  9. 237dmitry commented on Sep 28, 2026

    @237dmitry

    Enter-PSSession through PSRemoting

    I never managed to get PSRemoting working, not even over SSH. I still haven't figured out how to achieve this.

    [console]::Clear() works also fine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs-TriageThe issue is new and needs to be triaged by a work group.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions