Is there a way to have the PowerShell screen clear itself before each command?

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.

1

4 Answers

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.

1

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();.

1

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

1

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.

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like