Tips for Searching for PowerShell History

Posted on September 3, 2015 by Jeff Hicks in PowerShell with 0 Comments

In “Fishing for PowerShell: Leveraging Get-Command and Show-Command,” I offered some advice on a few PowerShell tools to help you find what you need. Naturally, PowerShell’s help system should be number one on your list, and I’m going to assume you already know how to use it. In this article, I want to focus on some commands you might not be aware of. Let’s look at command history.

Using Command Buffer to Find Command History

You can use the command buffer to grab a list of commands that you’ve previously used. This buffer only applies to the PowerShell console, not the ISE. You can access this buffer by pressing the F7 key.

Displaying Command Buffer with F7. (Image Credit: Jeff Hicks)

Displaying Command Buffer with F7. (Image Credit: Jeff Hicks)

You can scroll to find a command that’s been previously executed. If you hit Enter, PowerShell will run it. If you use the right arrow key, Windows will insert the command at the prompt where you can revise it if necessary. You can also type the first character of a command, and Windows will jump to the first previous command that starts with that character. If you know the item number, you can access it directly with F9.

Selecting a specific command with F9. (Image Credit: Jeff Hicks)

Selecting a specific command with F9. (Image Credit: Jeff Hicks)

Pressing Enter will insert the command at the prompt. If you don’t get any results or if you want to configure the settings, right-click on the system context menu and select Properties.

Configuring console properties. (Image Credit: Jeff Hicks)

Configuring console properties. (Image Credit: Jeff Hicks)

On the Options tab, you can configure Command History.

Configuring console options. (Image Credit: Jeff Hicks)

Configuring console options. (Image Credit: Jeff Hicks)

I’ve already changed the properties to increase the buffer size to 250, where the maximum value is 999. I’ve elected to use the Discard Old Duplicates setting so that any duplicate commands that I ran earlier are discarded. This ensures that the command buffer only contains unique commands. You should only have to configure the console window once.

Another interesting way to use the command buffer is with the F2 key. Let’s say I just executed this command.

Now I want to re-run it with some modifications. I could press the up arrow key, use F3, or select using F7. I can also press F2.

Selecting a partial command with F2. (Image Credit: Jeff Hicks)

Selecting a partial command with F2. (Image Credit: Jeff Hicks)

I enter in the | character and Windows inserts the previous command up to that character.

Inserting a partial command. (Image Credit: Jeff Hicks)

Inserting a partial command. (Image Credit: Jeff Hicks)

All of these commands are using the command buffer that’s part of the CMD.EXE window. Don’t confuse them with PowerShell history.

Sponsored

Using PowerShell’s Get-History cmdlet

PowerShell also maintains a separate command history that works in both the console and the ISE. You can access this history with the Get-History cmdlet, which has an alias of h.

Accessing PowerShell's Command History. (Image Credit: Jeff Hicks)

Accessing PowerShell’s Command History. (Image Credit: Jeff Hicks)

The number of items is controlled by the $MaximumHistoryCount variable. Although the defaults have gone up over the years, you can set your own value, most often in a PowerShell profile script.

When you find a command you want to re-run use the Invoke-History cmdlet or its alias r. To run command history item 35, I can simply type:

You can also search history from the command prompt. Type the # character, immediately followed by whatever text you want, and then press tab:

PowerShell will search history for all commands that contain that word. Use the tab key to cycle through them until you find the command you want.

One feature missing from the cmdlet is the ability to filter out duplicates. One way is to run the command like this:

Getting unique history. (Image Credit: Jeff Hicks)

Getting unique history. (Image Credit: Jeff Hicks)

You can see that IDs 3, 4, 8, 9, and 10 have been eliminated. If you like this, it’s trivial to create a simple function.

Or if you’d like something a bit more full featured, here’s a proxy version of Get-History that I created to not only allow you to get unique history items, but also to filter using simple or regular expression patterns.

With this tool, I can do things like this:

Using a proxy version of Get-History. (Image Credit: Jeff Hicks)

Using a proxy version of Get-History. (Image Credit: Jeff Hicks)

Finally, I’ve never had the need, but you can clear your history with the Clear-History cmdlet. You’ll get new history the next time you start a PowerShell session. I recommend taking a minute or two to read about the Clear-History cmdlet.

Sponsored

I’m hoping that having a few tools to help you find what you need will make your PowerShell work much more enjoyable and productive.

Sponsored

Tagged with