5

Question

Anybody knows if there is a shortcut to clear PowerShell screen and scrollback (equivalent of cls command) ?

More details with an example

Let say I have this:
Initial state

I know there is a Ctrl + L shortcut but that leaves scrollback, here is state after using it:
State after Ctrl + L

What I want is a shortcut which makes me in a state like after use of cls command where screen is clear and there is no more scrollback:
About to run cls command

State after cls

9
  • 1
    Have you tried CLS ? Works for me.
    – John
    Aug 30, 2023 at 12:44
  • 2
    @John I would assume that they want an actual Ctrl-key keyboard shortcut that can be done quickly and potentially in just two keys without having to type cls<enter>. They appear to know about cls already.
    – Mokubai
    Aug 30, 2023 at 12:48
  • 1
    (For what it's worth, *cls* is an alias for *Clear-Host*) Aug 30, 2023 at 12:48
  • 1
    CLS is as fast as any shortcut for me
    – John
    Aug 30, 2023 at 12:49
  • 1
    @John cls only works when the command interpreter is active.
    – Sneftel
    Aug 30, 2023 at 12:56

3 Answers 3

5

You could use the PSReadLine Module (typically installed with PowerShell). Using keyboard combo Ctrl+l, will run cls to clear the screen and scrollback buffer.

Set-PSReadLineKeyHandler -Chord Ctrl+l -ScriptBlock {
    [Microsoft.PowerShell.PSConsoleReadLine]::RevertLine()
    [Microsoft.PowerShell.PSConsoleReadLine]::Insert('cls')
    [Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()
}
1
  • 1
    Great, added it in my C:\Users\<myuser>\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 and works like a charm !
    – gluttony
    Sep 4, 2023 at 13:38
1

The "Clear buffer" command does this. By default that command has no key binding, so you'd have to type Ctrl-Shift-P to pull up the command palette and select it. However, you can customize the key bindings. Open the settings window with Ctrl-,, go to the Actions pane, add an action with the command "clear buffer", and pick whatever hotkey you'd like (I suggest Ctrl-L).

4
  • 3
    This answer is for Windows Terminal, not pure PowerShell.
    – harrymc
    Aug 30, 2023 at 13:21
  • @harrymc "Pure powershell" is not a GUI application. You can run PowerShell on Windows Terminal or on the "classic" Win7-era console. There is zero reason to do the latter.
    – Sneftel
    Aug 30, 2023 at 13:24
  • 4
    I do the zero reason all the time ...
    – harrymc
    Aug 30, 2023 at 13:25
  • One of the first things in my profile is to make an alias for Clear-Host: New-Alias 'c' Clear-Host -Force. @Sneftel - Server Core, pwsh (if on non-Windows OS), remoting, not every enterprise env allows you to install Windows Store apps, most enterprises aren't running Win11 yet - those are just a handful of examples among many others. The "classic Win7-era console" (console host, or conhost.exe) was updated in Windows 10 FYI. I bind CTRL+SHIFT+X to Clear Buffer in Windows Terminal so I can do it with one hand, and @jfrmilner's solution works in the console just as well.
    – Paul π
    Sep 3, 2023 at 13:42
1

You may use the free AutoHotkey.

The following example script will map F12 to the string "Clear-Host":

#IfWinActive ahk_exe powershell.exe
F12::Sendinput, Clear-Host{Enter}

After installing AutoHotKey, put the above text in a .ahk file and double-click it to test. You may stop the script by right-click on the green H icon in the traybar and choosing Exit. To have it run on login, place it in the Startup group at
C:\Users\USER-NAME\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup.

Useful AutoHotkey documentation:

1
  • While it is a good alternative I prefer jfrmilner's answer, still, upvoted yours too.
    – gluttony
    Sep 4, 2023 at 13:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .