15

I'm playing around with PowerShell and I find myself constantly typing cls before I run commands. Or worse, I run a command, realize that the output from other stuff is making it a pain to navigate, run cls, and rerun my darn command. Is there a way to have the previous command's output cleared before running each command? Obviously it would be ideal if this could be toggled with a command or something simple, but whatever would work. :) Also, I'm playing with PS 3, ISE, and PowerGUI.

Bohdan Kuts
  • 607
  • 15
  • 23
Programmer Paul
  • 488
  • 1
  • 3
  • 13

4 Answers4

29

Know this is older, but thought I'd pass this along to anyone else looking -

One trick I use is to simply put "cls" as the first line in my PowerShell editor screen (in my case, using PowerShell ISE). Then each time I run my script, the PowerShell console within the ISE is cleared out, saving time so only my most recent run is shown there.

I'm not advising to leave "cls" in production scripts - but it is helpful for iterative testing within an ISE where you are constantly checking output in the PowerShell console window based on script input.

wonea
  • 4,783
  • 17
  • 86
  • 139
Justin C
  • 647
  • 7
  • 11
10

From this article: In PowerShell, in order to clear the screen you can either type Clear-Host;, its aliases, cls; and clear; or its equivalent [System.Console]::Clear();.

Konstantinos
  • 103
  • 5
Anshul Garg
  • 313
  • 1
  • 3
  • 14
2

CLS, Clear and Clear-Host do not work when your powershell (or CMD for that matter) runs inside a PSEXEC session.

0

There is a function, prompt, that gets executed whenever PowerShell prompts for a command. On my system (PS v3, ISE) the built in script is

$host.ui.rawui.WindowTitle = ($host.ui.rawui.WindowTitle -split ":")[0] + ": $pwd"
$(if (test-path variable:/PSDebugContext) { 
    '[DBG]: ' 
  } else { 
    '' 
  }) + 
'PS' + 
$(if ($nestedpromptlevel -ge 1) { ">>>" } else { "> "})

You can replace or modify this script. For example you could put the cls command at the top of the script above.

wonea
  • 4,783
  • 17
  • 86
  • 139
Χpẘ
  • 3,403
  • 1
  • 13
  • 22
  • When I did that, it just clears the output of every command. So, that won't work. – Programmer Paul Oct 04 '13 at 16:49
  • I see. I thought you just wanted a pointer on which way to go. To flesh this out some more: you can put a read-host before the cls. Then you'd enter your command, the output would be displayed, you'd press enter to clear the output and enter your next command. If you think through modifying-the-prompt idea a bit, you could come up with some other ways to do it as well. – Χpẘ Oct 04 '13 at 19:04