Powershell history – don’t just press the up key

PowerShell

Written by:

Howdy folks! How are you today?

After my last post, I had to prepare for my trip to the USA, had to visit two of our offices and set up a new one. Unfortunately, the big California fire started… so things ware little tense during my stay, hope everyone is safe over there!

While working there, my colleague asks me about some cmdlets I ran in the last site we visited. I had a few Powershell Sessions open and I wasn’t sure in which one I ran the cmdlets, so I had to bring my investigator glasses.

A meme with a bad eel joke

Stagadish

So let’s talk about Powershell history. Technically, Powershell has two type of commands history. One is the command line buffer, which is part of the graphical terminal application. Second is the built-in command history feature, that provides detailed information about the commands you have run.

So what are the differences?

Command line buffer:

By default, the buffer remembers the last 50 commands typed in. Also, as we said before because is part of the graphical terminal application, it’s not limited to the current Powershell session (YES!). that means that every time we open a new session, we will have the same buffer, but know that if you already have two sessions running, you will have to reopen one of them in order to refresh the buffer.

How to use?

Up and Down keys:Navigate thru the buffer list, pressing the Up key will recall the last command you type, continue pressing the up key to move further in the list. Pressing the Down key will move the other way on the list.

F8: one of the coolest features in my opinion. You can’t view your buffer list, which makes it harder to find what you want, but with the help of F8, we can search thru the buffer. just type in the cmdlet you are looking for and press F8. Not what you looked for? press F8 again to further search the list.

how to change the buffer size?

We can increase the size of the buffer easily if we want, but pressing the right click on the Powershell Title Bar, Select “Defaults”, under the “Options” tab, change the value under “Buffer Size”

Buffer Size Properties menu

Powershell History Cmdlet

Windows Powershell itself keeps a history of the commands you have typed in. but this time its unique to the current session. this means that unlike the buffer, it won’t be shared across sessions, and won’t save after you close the session. the default history size is determined by the parameter $MaximumHistoryCount (from version 3.0 value set to 4096)

how to use?

Get-History

To view the command line history, use the following cmdlet:

We can also use the “h” alias if you want. Each cmdlet gets an ID in the order they executed. We can recall specific cmdlet from the history with it ID

cool tip: If we need to copy cmdlet from history to somewhere else, you can pipe it to “clip” and it will be ready in your clipboard.

To search the history, we can use the Select-String cmdlet:

If you need to see detailed information about each command you execute, you can use the Format-List:

If we need to save our history list for future purposes. we can export the list to CSV or XML files. When we export the history, the file includes the data that displayed when we format the history as a list, including all the details.

Add-History

We can use the Get-History cmdlet to import the history list we exported.

Clear History

If we need for some reasons to clear the history and start clean. we can do run the following cmdlet:

We can also delete specific cmdlet from history, using:

Invoke History

If we want to re-run a command from the history list, we can use the invoke history cmdlet. First, we need to check the ID of the cmdlet we want to run from history, then we can use the following:

Invoke-History example

Invoke-History

If needed we can run more then one cmdlet:

 

Combining everything:

So, we can view the Powershell history, export it, import it and run cmdlets from the list. That nice, but we can take it one step ahead. We worked in a session, made some trials and errors. and ended with a collection of cmdlets that fit our needs, its not a script material, but its a sequence of cmdlets we need to complete our task. we can use the history cmdlets to our advantage.

We can export the Powershell history list, and import it to any session we need. then we can invoke the cmdlets one by one. creating a “script” in seconds:

Summary:

Now that we know how to search the Powershell history, and use the buffer. We can be much more productive! I encourage you to try and explore all the cmdlets we talked about here. get familiar with it and add it to you skills set.

See you next time!

Comments are closed.