Powershell

How to Use the Clear-History (Microsoft.PowerShell.Core) Cmdlet in PowerShell?

The “Clear-History” cmdlet in PowerShell is used to erase records from the PowerShell command history. Each PowerShell session has its history. By default, the “Clear-History” cmdlet deletes the full history in the current PowerShell session. However, manually selected can also be deleted using parameters. “cls” is the usual alias for the “Clear-History” cmdlet.

This tutorial will provide the usage of PowerShell’s “Clear-History” cmdlet.

How to Utilize the PowerShell’s Clear-History Cmdlet?

As we described above, the “Clear-History” cmdlet is sued to remove the command history in the current PowerShell session. The history of the “PSReadLine” command does not get removed. To retrieve the command history in PowerShell, simply execute the “Get-History” cmdlet and then run the “Clear-History” cmdlet to delete it.

Syntax
The syntax for the “Clear-History” cmdlet is provided below:

Clear-History [-Id] [-Count] [CommonParameters]

Example 1: Use the Cmdlet “Clear-History” to Remove the Command History from PowerShell Session
Before clearing the history of the PowerShell console, first, create a history and check the number of entered commands by executing the given command:

Get-History

Let’s clear the history by executing the given command:

Clear-History

As you can see, the command history has been cleared successfully.

Let’s execute the given command to verify whether the history has been cleared or not:

Get-History

It can be seen that the history has cleared successfully. Only the recent command is visible, which we used to clear the history:

Example 2: Use the “Clear-History” Cmdlet to Delete the Newest Commands
First, use the “Clear-History” cmdlet, along with the “-Count” parameter having the value “5” assigned to it. After that, use the “-Newest” parameter to clear the new five entries entered by the user:

Clear-History -Count 5 -Newest

Example 3: Use the “Clear-History” Cmdlet to Delete Commands That Match Specific Criteria
To delete the command that matches the specific criteria, first, use the “Clear-History” cmdlet along with the “-CommandLine” parameter and assign it the “Help” value enclosed within wild characters. We used wild characters to match the specific commands:

Clear-History -CommandLine *Help*

That’s it! You have learned the usage of the “Clear-History” cmdlet in PowerShell.

Conclusion

The cmdlet “Clear-History” is utilized to remove the history of the executed commands from the current PowerShell session. To clear the command history, simply execute the “Clear-History” cmdlet or the “cls” alias in the PowerShell console. This guide has elaborated on the usage of PowerShell’s “Clear-History” cmdlet.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.