A Brief Guide to View, Manage, and Clear PowerShell History
PowerShell by default stores the history of all the previously-executed commands of the current session. This helps users quickly view and re-run the commands in the history, saving time and effort.
However, you may want to clear PowerShell history for security reasons or to reduce cluttering.
With this in mind, this article walks you through how to view and clear PowerShell history. In addition, it outlines various keyboard shortcuts to make the most of the PowerShell history. Let's dive in!
Quick Check
Part 1: Understanding the PowerShell Command History
PowerShell history is, as the name shows, a log of all the commands you have executed previously. This history can be accessed, deleted, or re-used by invoking a relevant cmdlet. The most obvious advantage of the PowerShell command history is to recall the earlier commands directly instead of typing them again.
The built-in PowerShell history is available for the current session only and is deleted when the session ends. PowerShell offers a range of cmdlets to handle the history more easily. For instance, if your command history includes sensitive information, execute the clear-history cmdlet to delete it permanently.
Key Takeaways
The basic cmdlets to manage PowerShell history include:
Command | Short Description |
---|---|
Clear-History | Clear the PowerShell command history of the ongoing session. |
Clear-History with -Id parameter | Remove a specific command from the history. |
Get-History | List the command history of the current session. |
Get-History with -Id parameter | To get a specific command from the PowerShell history. |
Invoke-History | Use a previously run command in the current session. |
$MaximumHistoryCount | Set the maximum number of commands to be saved in PowerShell history. |
Part 2: How to Clear PowerShell History
Before learning to clear the history of PowerShell commands, you should know how to view the command history.
1. View PowerShell Command History
The Get-History cmdlet retrieves the list of all the commands executed in the ongoing session. It displays relevant details of the commands including their text, IDs, and the execution order.
To view the list of previous commands and their details, run the command: Get-History
If you want to limit the number of commands from the history, use the "Count" flag. For instance, the "Get-History -Count 5" cmdlet will show the last 5 commands.
Similarly, you can also view the previous command by its ID. For instance, executing the command: Get-History -Id 20, will show the command with ID number 20. Similarly, the command Get-History -Count 1 will display the most recent command.
Get-History -Count 1
2. Clear All PowerShell History
It's fairly easy to clear the PowerShell history of the current session. The basic syntax for deleting PowerShell history is Clear-History, but you can specify the command ID or text to delete the unwanted commands only. This helps you better organize the PowerShell history, without cluttering it with irrelevant commands.
Example 1: Delete All Commands
Now, to clear all the previous commands of the current session, run the cmdlet:
Clear-History
Example 2: Clear PowerShell Specific Commands
Don't want to delete all the PowerShell commands? You can use "Clear-History" with the appropriate parameters to clear the specific commands.
For example, using the "ID" flag with the Clear-History cmdlet deletes that particular command. The command "Clear-History -Id 4" will delete the command with ID 4 from the list.
Clear-History -Id 4
If you want to delete multiple commands, type in the comma-separated list of Id numbers. The below command, for example, will delete two commands – ID 3 and ID 5:
Example 3: Clear PowerShell Latest History
The parameters Count and Newest let you delete the latest commands from the PowerShell's history. For instance, the cmdlet "Clear-History -Count 5 -Newest" clears the previous 5 commands only. In the figure below, it deletes the last 5 commands – from ID 6- ID 10.
Clear-History -Count 5 -Newest
Example 4: Clear PowerShell Commands Matching a Criteria
Similarly, you can specify to delete the commands containing a particular keyword through the "CommandLine" parameter. Suppose, you want to delete all the commands that include Help or end with Syntax.
This is done by running the command:
Clear-History -CommandLine *Help*, *Syntax
Example 5: Using Multiple Parameters to Clear PowerShell History
Similarly, you can also combine various parameters in the same cmdlet as per the requirements. For instance, using the "ID" and "Count" parameters delete the specified commands from history in reverse order - from newest to oldest.
Take a look at the below example:
Clear-History -Id 7 -Count 5
This command deletes 5 commands, starting from command with ID 7 in the reverse order. It will clear PowerShell history from Id 3 - Id 7.
3. Re-Use Previous PowerShell Commands
The "Invoke-History" cmdlet with various parameters is used to re-run commands from the history without bothering to type them again. For instance, if you wish to rerun the most recent command, enter:
Invoke-History
Similarly, you have the authority to run any specific command from the history. For that, add the "ID" parameter, like:
Invoke-History -Id 132
It will execute the command with ID 132.
4. Maximum PowerShell History Commands
With PowerShell 3 on Windows, the default number of commands it stores for the current session is 4096. If you want to modify this number, execute the $MaximumHistoryCount cmdlet with the appropriate parameter.
For instance, to reduce the number of commands in PowerShell history to 100:
$MaximumHistoryCount = 100
(Restart PowerShell to apply the settings)
And if you don't want PowerShell to store the command history, run:
$MaximumHistoryCount = 1
It will now only store the last command you have executed.
Part 3: Keyboard Shortcuts for Efficiently Managing PowerShell History
When using the PowerShell console, use the following keyboard shortcuts to quickly manage the PowerShell history:
Up and Down Arrows: For the previous command, press the up arrow while hit the down arrow to get next command.
F7: Get the command history of the current session.
ESC: To hide the history.
F8: To search for command history. Type in one or more characters and hit F8. Press F8 again to show the next matching command.
F9: Search the command by its ID. Press F9 and type in the command ID. (Use F7 to find the IDs of all the commands).
Bonus Tip: Use AirDroid Remote Support to Access and Control Remote Devices
Do you wish to remotely manage the PowerShell history and other tasks on your Windows PC from anywhere? AirDroid Remote Support tops the list of the most efficient remote software in 2024. It's among the pioneer apps that give secure access to both attended and unattended devices.
Its list of features include:
- Remote Control: Gives uninterrupted control over the Windows and Android devices to complete your tasks remotely. It works seamlessly on low bandwidth and low-end PCs as well.
- Black Screen: The admin has the freedom to display a black screen on the controlled device for privacy.
- File Transfer: Transfer files between the host and remote devices at a decent speed.
- Screen Sharing: Real-time screen sharing of the remote device to provide instructions or customer service.
- Flexible Pricing Plans: Comes in a variety of premium plans, all lower than its competitors.
With its advanced collaboration and remote management features, it caters to the needs of both individuals and business owners.
FAQs
Q1: How many commands PowerShell stores in the current session?
PowerShell by default maintains a history of commands up to a maximum of 4096 for the current session. However, you can customize the number by executing the $MaximumHistoryCount cmdlet.
Q2: Is the command history in PowerShell accessible after closing the session?
Unfortunately, no. The built-in memory of PowerShell stores the command history for the current session only. When the session is closed, the history is inaccessible.
Final Remarks
This article shows that clearing the PowerShell history is a straightforward process. Use the Clear-History cmdlet with the appropriate parameters to organize the PowerShell command history more efficiently and safeguard privacy.
Moreover, you can use AirDroid Remote Support to control to Windows, Android, and iOS devices from anywhere. It is lightweight, inexpensive, and offers plenty of stellar functionalities to exert complete control over the remote device.
Thanks
Leave a Reply.