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:
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:
Let’s clear the history by executing the given command:
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:
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:
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:
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.