PowerShell: Clear History of Previous Commands

This is not obvious, but the Clear-History command in PowerShell won’t clear the history of the previous commands.

The Clear-History clears only the commands entered during the current session, that could be displayed by the Get-History command.

To clear the history in PowerShell, it needs to delete the file in which the previous commands are stored.

In this note i am showing how to locate the history file and how to clear the history of the PowerShell commands.

Cool Tip: PowerShell commands history file location! Read more →

Clear History in PowerShell

Get the PowerShell command history file location:

PS C:\> (Get-PSReadlineOption).HistorySavePath

Show the contents of the PowerShell command history file:

PS C:\> cat (Get-PSReadlineOption).HistorySavePath

Clear the command history in PowerShell by deleting the history file:

PS C:\> Remove-Item (Get-PSReadlineOption).HistorySavePath

Change how PowerShell command history is saved:

PS C:\> Set-PSReadlineOption -HistorySaveStyle SaveIncrementally # default
PS C:\> Set-PSReadlineOption -HistorySaveStyle SaveAtExit
PS C:\> Set-PSReadlineOption -HistorySaveStyle SaveNothing
Was it useful? Share this post with the world!

15 Replies to “PowerShell: Clear History of Previous Commands”

  1. This is elegant and simple. Thanks.

  2. Excellent. Just what I was looking for.

  3. very helpfull, thanks alot

  4. Remove-Item (Get-PSReadlineOption).HistorySavePath; Clear-History
    deletes even the Remove-Item command 😉

  5. I just want to delete the wrong command, opening the ConsoleHost_history.txt file to delete is not elegant

  6. Helpfull for me too

  7. I did both of those commands, but hitting the up arrow still shows my previous commands. Is there any way to clear for the current session or must I close Powershell and start a new session to have a clear history?

  8. Yes, close and open again

  9. Take a bow. It worked beautifully. Straight, sweet and right to the point. Thanks.

  10. How get the event in event viewer?

  11. did not work

  12. Thank you so much! First hint which is working. A charme.

Leave a Reply