Windows Tip Sheet

PowerShell History Lesson

Double the PowerShell command buffer -- your fingers will thank you for this little tip.

If you've spent any time at a command prompt, you likely know about the command buffer. PowerShell also has a command buffer which, by default, will store the last 64 commands you've entered. You can change that by modifying the $MaximumHistoryCount variable:


If you add this line to your profile, you'll double the number of saved commands. To see where you've been, use the History alias or the Get-History cmdlet. If you really dislike typing, you can shorten the alias to "h".

The cmdlet will show you the last 32 commands. You can specify the number of commands to see like this:

History –count 50

To see the entire contents of your command history try this:

history -count $maximumhistorycount | more

History information is stored in a HistoryInfo object. One of its properties is commandline. This means you can find related commands:

h | where {$_.commandline -match "service"}

Now, you'll be able to see all the commands with the word "service" in the command, including the history id.

To run a saved command, use the Invoke-History cmdlet and specify the ID number you saw from the History command:

Invoke-History 1340

This runs the command with id 1340. You can also use the aliases r and ihy.

When you execute a previous command using Invoke-History, PowerShell will treat it as if you typed it for the first time. This means you can pipe the output to other cmdlets. Let me show you.

On my computer, command id 1359 is "gwmi win32_service". I can reuse this command in a pipeline:

r 1359 | where {$_.State -eq "stopped"} | Select Name

Tech Help—Just An
E-Mail Away



The output of the first expression is replaced with "gwmi win32_service," so I get a list of all stopped services. Granted, this saved command was pretty simple to type. Once you start building long and complex commands, being able to recall them easily and quickly will make a major difference in your PowerShell experience.

About the Author


comments powered by Disqus