Do you use a Windows PC or laptop and want to find out the right way to clear Command History in PowerShell and CMD Screen? If yes, then you’re in the right place because this guide will be of great help to you. In this guide, you will get to know how to clear Command History in PowerShell and CMD Screen.
Command Prompt (CMD) and PowerShell tools are typically used for system administration, automation, and troubleshooting. However, when you enter multiple commands one after the other, it can make the screen messy. Sometimes, you have no option but to declutter the command window screen and start fresh.
Clearing the command history in PowerShell and CMD Screen will be helpful in improving readability, avoiding confusion, maintaining privacy, and establish a clear workflow in command-line interfaces.
How to clear command history in PowerShell?
It is quite easy to clear all the commands and their outputs in the current PowerShell session using PowerShell cmdlets and keyboard shortcuts.
First open cmd or powershell
To clear PowerShell screen, you need to type the following cmdlet in PowerShell and then press Enter –
Here we are using adb devices command just to showcase,
adb devices
If adb devices didn’t work on powershell, type ./adb devices
Clear-Host
This would clear the command window screen.
Alternatively, you can even use the aliases of the Clear-Host cmdlet for clearing the PowerShell console. You just need to type “cls”, “cls/?” or “clear” and then press Enter in the PowerShell window. This would remove all the previous commands and outputs from the screen, allowing you to start fresh.
Moreover, you can even use keyboard shortcuts for clearing the PowerShell screen in Windows 10/11. Pressing the Ctrl + L buttons simultaneously on your keyboard will be the same as executing Clear-Host.
While “Clear-Host” will clear out the current console, the Clear-History cmdlet and Alt + F7 combination is used for clearing the command history in PowerShell. If the PowerShell’s Clear-History isn’t working for you and it does not have the command history, it is because PowerShell’s history gets saved in a text file named “ConsoleHost_history.txt” at the location mentioned below –
C:UsersuserAppDataRoamingMicrosoftWindowsPowershellPSReadLine
Now, you can use the following command and you will get the location of the command history text file in Windows 10/11.
(Get-PSReadLineOption).HistorySavePath
You’ll be able to either navigate to the above location on your PC and then remove the content of the “ConsoleHost_history.txt” file or simply execute the following command for clearing the command history in PowerShell.
[Microsoft.PowerShell.PSConsoleReadLine]::ClearHistory()
You can even instruct PowerShell not to save command history in the ConsoleHost_history.txt at all. To do that, use the following command –
Set-PSReadLine.Option -HistorySaveStyle SaveNothing
For enabling the saving of command history to the text file, you should use the following command –
Set-PSReadLineOption -HistorySavevStyle SaveIncrementally
How to clear CMD Screen and History in Command Prompt?
You can either clear a single line or all the commands from the Command Prompt screen and then start with a clean slate.
For clearing the Command Prompt screen on your Windows PC, you should type cls/CLS in the command prompt and then press “Enter”.
Here are some of the shortcut keys that will help you while using the Command Prompt –
- Esc/Escape – If you accidentally type a wrong command, you can quickly clear the line from the CMD screen by pressing the Esc key
- Ctrl + Backspace – This will delete one left to the cursor
- Ctrl + C – Leave the line you’re typing or terminate the command in process and begin with a new prompt
- Alt + F4 – This will close the PowerShell or Command Prompt window
Command history is an extremely useful feature in the Command Prompt. It will allow you to access your past commands quickly and then reuse them when required. You’ll be able to view the commands used in the current session in multiple ways –
- Up/down arrow keys – Cycle through your previously given commands
- F7 – Displays the list of commands used in the CMD
You’ll be able to use the up and down arrow keys for highlighting the commands and press “Enter” for using them.
If you wish to clear the Command prompt history on Windows PC, you should press Alt + F7. The command history will be cleared.
We hope this guide helped you find out how to clear command history in PowerShell and CMD Screen. If you have any questions or suggestions, feel free to mention them in the comments section.