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:

$MaximumHistoryCount=128

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

Got a Windows, Exchange or virtualization question or need troubleshooting help? Or maybe you want a better explanation than provided in the manuals? Describe your dilemma in an e-mail to the MCPmag.com editors at [email protected]; the best questions get answered in this column and garner the questioner with a nifty Redmond T-shirt.

When you send your questions, please include your full first and last name, location, certifications (if any) with your message. (If you prefer to remain anonymous, specify this in your message, but submit the requested information for verification purposes.)

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

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular

Upcoming Training Events