about_History
Short description
Describes how to get and run commands in the command history.
Long description
When you enter a command at the command prompt, PowerShell saves the command in the command history. You can use the commands in the history as a record of your work. And, you can recall and run the commands from the command history.
PowerShell has two different history providers: the built-in history and the history managed by the PSReadLine module. The histories are managed separately, but both histories are available in sessions where PSReadLine is loaded.
Using the PSReadLine history
The PSReadLine history tracks the commands used in all PowerShell sessions.
The history is written to a central file per host. That history file is
available to all sessions and contains all past history. The history is not
deleted when the session ends. Also, that history cannot be managed by the
*-History
cmdlets. For more information, see
about_PSReadLine.
Using the built-in session history
The built-in history only tracks the commands used in the current session. The history is not available to other sessions and is deleted when the session ends.
History Cmdlets
PowerShell has a set of cmdlets that manage the command history.
Cmdlet | Alias | Description |
---|---|---|
Get-History |
h |
Gets the command history. |
Invoke-History |
r |
Runs a command in the command history. |
Add-History |
Adds a command to the command history. | |
Clear-History |
clhy |
Deletes commands from the command history. |
Keyboard Shortcuts for Managing History
In the PowerShell console, you can use the following shortcuts to manage the command history.
- UpArrow - Displays the previous command.
- DownArrow - Displays the next command.
- F7 - Displays the command history.
- ESC - To hide the history.
- F8 - Finds a command. Type one or more characters then press F8. Press F8 again the next instance.
- F9 - Find a command by history ID. Type the history ID then press F9. Press F7 to find the ID.
- #
<string>
Tab - Search the history for*<string>*
and returns the most recent match. If you press Tab repeatedly, it cycles through the matching items in your history.
Note
These key bindings are implemented by the console host application. Other applications, such as Visual Studio Code or Windows Terminal, can have different key bindings. The bindings can be overridden by the PSReadLine module. PSReadLine loads automatically when you start a PowerShell session. With PSReadLine loaded, F7 and F9 are not bound to any function. PSReadLine does not provide equivalent functionality. For more information, see about_PSReadLine.
MaximumHistoryCount
The $MaximumHistoryCount
preference variable determines the maximum number of
commands that PowerShell saves in the command history. The default value is
4096.
For example, the following command lowers the $MaximumHistoryCount
to 100
commands:
$MaximumHistoryCount = 100
To apply the setting, restart PowerShell.
To save the new variable value for all your PowerShell sessions, add the assignment statement to a PowerShell profile. For more information about profiles, see about_Profiles.
For more information about the $MaximumHistoryCount
preference variable, see
about_Preference_Variables.
Order of Commands in the History
Commands are added to the history when the command finishes executing, not when the command is entered. If commands take some time to be completed, or if the commands are executing in a nested prompt, the commands might appear to be out of order in the history. Commands that are executing in a nested prompt are completed only when you exit the prompt level.