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.

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