Windows PowerShell has a built-in ascendance history characteristic that provides detailed data most the commands you've run. Like the Command Prompt, PowerShell exclusively remembers your ascendance history for the electrical current session.
How to Use the Command-Line Buffer
RELATED: Geek School: Learn How to Automate Windows amongst PowerShell
PowerShell technically has 2 types of ascendance history. First, there's the commandline buffer, which is genuinely business office of the graphical PowerShell terminal application as well as non business office of the underlying Windows PowerShell application. It provides a few basic features:
- Up Arrow: Recall the previous ascendance y'all typed. Press the telephone substitution repeatedly to walk through your ascendance history.
- Down Arrow: Recall the side yesteryear side ascendance y'all typed. Press the telephone substitution repeatedly to walk through your ascendance history.
- F8: Search your ascendance history for a ascendance matching the text on the current command line. So, if y'all wanted to search for a ascendance that began amongst "p", you'd type "p" on the ascendance business as well as and then repeatedly tap F8 to wheel through commands inwards your history that start out amongst "a".
By default, the buffer remembers the terminal 50 commands y'all typed. To alter this, right-click the championship bar of the PowerShell prompt window, conduct "Properties", as well as alter the value of "Buffer Size" nether Command History.
How to View PowerShell History
Windows PowerShell itself keeps a history of the commands you've typed inwards the electrical current PowerShell session. You tin purpose several included cmdlets to thought as well as function amongst your history.
To thought the history of commands you've typed, run the next cmdlet:
Get-History
You tin search your history yesteryear pipage the resulting output to the Select-String
cmdlet as well as specifying the text y'all desire to search for. Replace "Example" inwards the cmdlet below amongst the text y'all desire to search for:
Get-History | Select-String -Pattern "Example"
To thought a to a greater extent than detailed ascendance history that displays the execution condition of each ascendance along amongst its start as well as halt times, run the next command:
Get-History | Format-List -Property *
By default, the Get-History
cmdlet exclusively shows the 32 most recent history entries. If y'all desire to thought or search a larger publish of history entries, purpose the -Count
option to specify how many history entries PowerShell should show, similar so:
Get-History -Count M Get-History -Count M | Select-String -Pattern "Example" Get-History -Count M | Format-List -Property *
How to Run Commands From Your History
To run a ascendance from your history, purpose the next cmdlet, specifying the Id publish of the history item equally shown yesteryear the Get-History
cmdlet:
Invoke-History #
To run 2 commands from your history dorsum to back, use Invoke-History
twice on the same line, separated yesteryear a semicolon. For example, to apace run the start ascendance inwards your history as well as 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 next cmdlet:
Clear-History
Note that the ascendance business buffer is dissever from the PowerShell history. So, fifty-fifty after y'all run Clear-History
, y'all tin overstep away along to press the upwardly as well as downwards arrow keys to scroll through commands you've typed. However, if y'all run Get-History
, you'll run across that your PowerShell history is inwards fact empty.
PowerShell doesn't yell back your history betwixt sessions. To erase both ascendance histories for the electrical current session, all y'all accept to create is closed the PowerShell window.
If you'd similar to clear the PowerShell window after clearing the history, y'all tin create it yesteryear running the Clear
command:
Clear
How to Save as well as Import Your PowerShell History
If y'all desire to salve the PowerShell ascendance history for the electrical current session thence y'all tin refer to it later, y'all tin create so.
Get-History | Export-Clixml -Path c:\users\name\desktop\commands.xml
This exports your ascendance history equally a detailed XML file consummate amongst "StartExecutionTime" as well as "EndExecutionTime" values for each ascendance that nation y'all when the ascendance was run as well as how long it took to complete.
Once you've exported your PowerShell history to such an XML file, y'all (or anyone else y'all transportation the XML file to) tin import it to simply about other PowerShell session amongst the Add-History cmdlet:
Add-History -InputObject (Import-Clixml -Path C:\users\name\desktop\commands.xml)
If y'all run the Get-History
cmdlet after importing such an XML file, you'll run across that the commands from the XML file were imported into your electrical current PowerShell session's history.