Windows PowerShell has a built-in command history feature that provides detailed information about the commands you’ve run. Like the Command Prompt, PowerShell only remembers your command history for the current session.
How to Use the Command-Line Buffer
RELATED: Geek School: Learn How to Automate Windows with PowerShell
PowerShell technically has two types of command history. First, there’s the commandline buffer, which is actually part of the graphical PowerShell terminal application and not part of the underlying Windows PowerShell application. It provides a few basic features:
- Up Arrow: Recall the previous command you typed. Press the key repeatedly to walk through your command history.
- Down Arrow: Recall the next command you typed. Press the key repeatedly to walk through your command history.
- F8: Search your command history for a command matching the text on the current command line. So, if you wanted to search for a command that began with “p”, you’d type “p” on the command line and then repeatedly tap F8 to cycle through commands in your history that begin with “a”.
By default, the buffer remembers the last 50 commands you typed. To change this, right-click the title bar of the PowerShell prompt window, select “Properties”, and change the value of “Buffer Size” under Command History.
How to View PowerShell History
Windows PowerShell itself keeps a history of the commands you’ve typed in the current PowerShell session. You can use several included cmdlets to view and work with your history.
To view the history of commands you’ve typed, run the following cmdlet:
You can search your history by piping the resulting output to the
Select-String cmdlet and specifying the text you want to search for. Replace “Example” in the cmdlet below with the text you want to search for:
Get-History | Select-String -Pattern "Example"
To view a more detailed command history that displays the execution status of each command along with its start and end times, run the following command:
Get-History | Format-List -Property *
By default, the
Get-History cmdlet only shows the 32 most recent history entries. If you want to view or search a larger number of history entries, use the
-Count option to specify how many history entries PowerShell should show, like so:
Get-History -Count 1000 Get-History -Count 1000 | Select-String -Pattern "Example" Get-History -Count 1000 | Format-List -Property *
How to Run Commands From Your History
To run a command from your history, use the following cmdlet, specifying the Id number of the history item as shown by the
To run two commands from your history back to back, use
Invoke-History twice on the same line, separated by a semicolon. For example, to quickly run the first command in your history and then the second, you’d run:
Invoke-History 1;Invoke-History 2
How to Clear Your PowerShell History
To clear the history of commands you’ve typed, run the following cmdlet:
Note that the command line buffer is separate from the PowerShell history. So, even after you run
Clear-History, you can continue to press the up and down arrow keys to scroll through commands you’ve typed. However, if you run
Get-History, you’ll see that your PowerShell history is in fact empty.
PowerShell doesn’t remember your history between sessions. To erase both command histories for the current session, all you have to do is close the PowerShell window.
If you’d like to clear the PowerShell window after clearing the history, you can do it by running the
How to Save and Import Your PowerShell History
If you want to save the PowerShell command history for the current session so you can refer to it later, you can do so.
Get-History | Export-Clixml -Path c:\users\name\desktop\commands.xml
This exports your command history as a detailed XML file complete with “StartExecutionTime” and “EndExecutionTime” values for each command that tell you when the command was run and how long it took to complete.
Once you’ve exported your PowerShell history to such an XML file, you (or anyone else you send the XML file to) can import it to another PowerShell session with the Add-History cmdlet:
Add-History -InputObject (Import-Clixml -Path C:\users\name\desktop\commands.xml)
If you run the
Get-History cmdlet after importing such an XML file, you’ll see that the commands from the XML file were imported into your current PowerShell session’s history.