How to Use Your Command History in Windows PowerShell

0
1087
Loading...

Windows PowerShell has a integrated command historical past characteristic that gives detailed details about the instructions you’ve run. Like the Command Prompt, PowerShell most effective recollects your command historical past for the present consultation.

How to Use the Command-Line Buffer

PowerShell technically has two sorts of command historical past. First, there’s the commandline buffer, which is in fact a part of the graphical PowerShell terminal software and now not a part of the underlying Windows PowerShell software. It supplies a couple of elementary options:

  • Up Arrow: Recall the former command you typed. Press the important thing many times to stroll thru your command historical past.
  • Down Arrow: Recall the following command you typed. Press the important thing many times to stroll thru your command historical past.
  • F8: Search your command historical past for a command matching the textual content at the present command line. So, in case you sought after to seek for a command that started with “p”, you’d sort “p” at the command line after which many times faucet F8 to cycle thru instructions on your historical past that start with “a”.

By default, the buffer recollects the remaining 50 instructions you typed. To exchange this, right-click the name bar of the PowerShell steered window, make a selection “Properties”, and alter the worth of “Buffer Size” underneath Command History.

How to View PowerShell History

Windows PowerShell itself helps to keep a historical past of the instructions you’ve typed within the present PowerShell consultation. You can use a number of integrated cmdlets to view and paintings along with your historical past.

To view the historical past of instructions you’ve typed, run the next cmdlet:

Get-History

You can seek your historical past by means of piping the ensuing output to the Select-String cmdlet and specifying the textual content you wish to have to seek for. Replace “Example” within the cmdlet under with the textual content you wish to have to seek for:

Get-History |  Select-String -Pattern "Example"

To view a extra detailed command historical past that shows the execution standing of every command in conjunction with its get started and finish instances, run the next command:

Get-History | Format-List -Property *

By default, the Get-History cmdlet most effective displays the 32 most up-to-date historical past entries. If you wish to have to view or seek a bigger choice of historical past entries, use the -Count technique to specify what number of historical past entries PowerShell will have to display, 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 out of your historical past, use the next cmdlet, specifying the Id choice of the historical past merchandise as proven by means of the Get-History cmdlet:

Invoke-History #

To run two instructions out of your historical past again to again, use Invoke-History two times at the similar line, separated by means of a semicolon. For instance, to briefly run the primary command on your historical past after which the second one, you’d run:

Invoke-History 1;Invoke-History 2

How to Clear Your PowerShell History

To transparent the historical past of instructions you’ve typed, run the next cmdlet:

Clear-History

Note that the command line buffer is break free the PowerShell historical past. So, even after you run Clear-History, you’ll be able to proceed to press the up and down arrow keys to scroll thru instructions you’ve typed. However, in case you run Get-History, you’ll see that your PowerShell historical past is in truth empty.

PowerShell doesn’t consider your historical past between periods. To erase each command histories for the present consultation, all you must do is shut the PowerShell window.

If you’d love to transparent the PowerShell window after clearing the historical past, you’ll be able to do it by means of operating the Clear command:

Clear

How to Save and Import Your PowerShell History

If you wish to have to avoid wasting the PowerShell command historical past for the present consultation so you’ll be able to seek advice from it later, you’ll be able to accomplish that.

Get-History | Export-Clixml -Path c:usersnamedesktopcommands.xml

This exports your command historical past as an in depth XML record entire with “StartExecutionTime” and “EndExecutionTime” values for every command that inform you when the command used to be run and the way lengthy it took to finish.

Once you’ve exported your PowerShell historical past to such an XML record, you (or someone else you ship the XML record to) can import it to some other PowerShell consultation with the Add-History cmdlet:

Add-History -InputObject (Import-Clixml -Path C:usersnamedesktopcommands.xml)

If you run the Get-History cmdlet after uploading such an XML record, you’ll see that the instructions from the XML record have been imported into your present PowerShell consultation’s historical past.

Chris Hoffman is a era creator and all-around pc geek. He’s as at house the use of the Linux terminal as he’s digging into the Windows registry. Connect with him on Google+.