Share this post:This blog post is part of an ongoing series by Adam Gordon. Adam will guide you through a PowerShell command each week, explaining when and how to use them. Clear-History is Adam’s topic this week.
Clear-History: When should it be used?
Clear-History removes all command history from a PowerShell session. Each PowerShell session has a unique command history. Use the Get-History cmdlet to display the command history.
Clear-History will delete all command history from PowerShell sessions by default. Clear-History can be configured to delete specific commands.
Clear-History will not clear the PSReadLine command log history file. The history file containing every PowerShell command is stored in the PSReadLine module.
To scroll through the command history, use the up- and down arrows at your keyboard from a PowerShell prompt.
To display the PSReadLine configuration for command history, use Get-PSReadLineOption. PSReadLine ships with PowerShell 5.0 or higher.
What version of PowerShell should I use for this blog?
Get the PowerShell Version for your machine
$PSVersionTable
This command displays the PowerShell version information for your machine.
Clear-History: How do you use it?
You can delete the command history of a PowerShell session.
1st command: Get-History
2nd command: Clear-History
3rd command: Get-History
The Get-History cmdlet displays PowerShell session history.
Clear-History erases all command history. Get-History displays the current command history and confirms that the previous history was deleted.
You can delete commands that meet specific criteria.
1st command: Get-History
2nd command: Clear-History -CommandLine *Help*
3rd command: Get-History
The Get-History cmdlet displays PowerShell session history.
Clear-History erases command history.
The -CommandLine parameter indicates commands that include Help.
Get-History displays an updated command history and confirms that commands Id 4 and Id 5 have been deleted.
Delete commands by Id number
1st command: Get-History
2nd command: Clear-History -Id 1,3,7
3rd command: Get-History
The Get-History cmdlet displays PowerShell session history.
Clear-History erases command history.
The -Id parameter indicates which commands to delete.
Get-History displays the most recent command history and confirms that Id 1, Id 3 & Id 7 have been deleted.
Do you need PowerShell training? ITProTV offers PowerShell online IT training courses.