PowerShell Command History
In this tutorial we will see about PowerShell History. We will see want information is kept in history, how we can access and use our history. Every command you run in Windows PowerShell, it is added in the command history. The command is added in the history after the execution of the command has been completed. If a command takes a lot of time to complete the execution, you will not be able to see the command in the command history list unless it has completed the action. PowerShell command history can be displayed in console, or exported in a file. You can use your command history for evidence of you work or reference for procedures that you repeat. In order to see the history you have to use the below command.
Maximum History Count
The default maximum history count of Windows PowerShell is 4096 (PowerShell 3.0 and above). Until PowerShell 2.0 the default limit was only 64. The limit is defined by a preference variable called $MaximumHistoryCount. You are allowed to change the limit by setting the number of the variable to a different one. The accepted range is from 1 to 32767. You can set the limit to a different one by using the command.
$MaximumHistoryCount = [number]
The below example sets the history limit to 1000.
$MaximumHistoryCount = 1000
The below example sets the limit to 20000.
$MaximumHistoryCount = 20000
Please note, in order for the new setting to be applied, you need to restart Windows PowerShell. This will apply the new setting only to the current session. You are able also to change the limit on all your sessions. In order to do that you will need to perform the change in Windows PowerShell profile. I will not discuss further on how this can be applied on profile level, but you are able to find more information on Microsoft.
The are a few cmdlets that are related to PowerShell command history. We will see each one of the with examples so you will be able to understand better.
Get-History cmdlet will get the history of commands in the current session. By using the command alone it will display the information in the console. You can also export the output to a file or by using
Out-GridView. The default alias of the command is
h. In the below screenshots you will see the output of the cmdlet and the use of the alias.
Invoke-History cmdlet, you will be able to run a command in the command history. The default alias of the command is
r. The command needs to be specified by the command id number in the history. The format of the command is
Invoke-History 1 or
r 1. The below examples show you the use of this command.
Add-History cmdlet adds a command in the history. The way that you are able to use this command is show in the below example. One way of using the command is to add a list of commands that you have already exported to the session. In the below example, I get the current command history and then add the list retrieved to the history again. The command that I have used is the below:
Get-History | Add-History
Clear-History cmdlet clear the history of the current session. The alias of this cmdlet is
clhy. Please note that the id number of the commands in history is not reset and will continue from the number that it was left of. The below example show the use of the command.
There are some keyboard shortcuts that allow you to display, find and run commands from the history. Let’s check them.
Up Arrow: The Up Arrow key displays the previous command in history.
Down Arrow: The Down Arrow key displays the next command in history.
F7: Displays the history in the console.
ESC: Hides history from console.
F8: Finds a command from the history. You need to type few letters and then hit F8. If there are more that one commands in the history that matching those letters, then by hitting F8 again, it will display the next command.
F9: Finds a command by the id number and by pressing ENTER, it will put the command in the console ready to run.
I hope the tutorial about PowerShell command history is helpful.
Please let me know your comments and thoughts. You feedback is appreciated.
- PowerShell Tutorials
- PowerShell Scripts
- about_History | Microsoft Docs
- Get-History – Microsoft Docs
- Invoke-History – Microsoft Docs
- Add-History – Microsoft Docs
- Clear-History – Microsoft Docs