How to clear the Powershell command history

Case

You have typed a lot of Powershell cmdlets and scripts and need to clean the Powershell command history in order to eradicate all instances of variables including passwords in clear text.

Solution

Running Clear-History is not sufficient as this will not clear the running host’s command history. This command history is saved in a text file by using the Powershell PSReadLine module. PSReadLine maintains a history file containing all the commands and data you have entered from the command line. The history files is a file named $($host.Name)_history.txt. On Windows systems the history file is stored at $env:APPDATA\Microsoft\Windows\PowerShell\PSReadLine. On non-Windows systems, the history files is stored at $env:XDG_DATA_HOME/powershell/PSReadLine or $env:HOME/.local/share/powershell/PSReadLine.

You will need to run the following commands to fully clear the Powershell command history:

Clear-History
Remove-Item "$env:APPDATA\Microsoft\Windows\PowerShell\PSReadLine\*"

Was this article helpful?

Related Articles