Clear-History | Taking on PowerShell one cmdlet at a time | Weekly Blog

Share this post:

This is a part of an on-going blog series written by Adam Gordon. Each week, Adam will walk you through a PowerShell command, showing you when and how to use each one. This week, Adam covers Clear-History. 

When to use Clear-History?

Clear-History deletes the command history from a PowerShell session. Each PowerShell session has its own command history. To display the command history, use the Get-History cmdlet.

By default, Clear-History deletes the entire command history from a PowerShell session. You can use parameters with Clear-History to delete selected commands.

Clear-History does not clear the PSReadLine command history file. The PSReadLine module stores a history file that contains every PowerShell command from every PowerShell session.

From a PowerShell prompt, use the up and down arrows on your keyboard to scroll through the command history.

To display the PSReadLine configuration for command history, use Get-PSReadLineOption. PSReadLine shipped with PowerShell 5.0 and above.

What version of PowerShell am I using for this blog?

Get the PowerShell Version from your machine:

$PSVersionTable

This command shows you the PowerShell version information on your machine.

How to use Clear-History?

Delete the command history from a PowerShell session:

1st command: Get-History

2nd command: Clear-History

3rd command: Get-History

The Get-History cmdlet displays the PowerShell session’s history.

Clear-History deletes the entire command history. Get-History displays the updated command history and confirms the prior history was deleted.

Delete commands that match specific criteria: 

1st command: Get-History

2nd command: Clear-History -CommandLine *Help*

3rd command: Get-History

The Get-History cmdlet displays the PowerShell session’s history.

Clear-History deletes the command history.

The –CommandLine parameter specifies commands that contain Help.

Get-History displays the updated command history and confirms that commands Id 4, and Id 5 were 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 the PowerShell session’s history.

Clear-History deletes the command history.

The –Id parameter specifies which commands to delete.

Get-History displays the updated command history and confirms that Id 1, Id 3 and Id 7 were deleted.

Need PowerShell training? Check out ITProTV’s PowerShell online IT training courses.

Leave a Reply

Your email address will not be published. Required fields are marked *