Clear History - PowerShell


Clear-History is a PowerShell command used to delete all or a specific number of commands from the command history. This helps in clearing any sensitive or confidential commands from the history, maintaining privacy and keeping the history list tidy.


Clear-History [-Count ]


-Count : Specifies the number of commands to delete from the history. Without this parameter, all commands in the history will be deleted.


Example 1: Clear the entire command history:


Example 2: Clear the last 5 commands in the history:

Clear-History -Count 5

Common Issues

Empty Command History: If the command history is already empty, executing Clear-History will result in an error.

Solution: Ensure that there are commands in the history before using Clear-History.


Clear-History can be integrated with other PowerShell commands to automate tasks:

# Clear the history and export the remaining commands to a file
Clear-History -Count 10 | Out-File -FilePath .\history.txt
  • Get-History: Retrieves commands from the command history.
  • Invoke-History: Executes a specified command from the history.
  • Remove-History: Removes specific commands from the history by index or command text.