#PSTip Using Tab completion against the command history

Note: This tip requires PowerShell 2.0 or above.

Each time you execute a command in Windows PowerShell, the command is remembered and can be fetched by using the Get-History cmdlet or by using the the up/down arrow keys.  One great, less-known, time-saver feature, allows you to quickly cycle through commands using tab completion. For example, to recall the first command containing the string ‘dir’, type the ‘#’ sign followed by the pattern of the command you want to find, and then press the Tab key:

PS> #dir<TAB>

You should now see the expansion of the first command from your history that matches the pattern.
Keep pressing the TAB key to cycle through all commands that match the specified pattern.

If you know the exact command ID you can quickly retrieve it by specifying the ‘#’ sign followed by the ID:

PS> #11<TAB>

About the author: Shay Levy

Shay Levy is a Co-founder and editor of the PowerShell Magazine. He is a multiple-year recipient of the Microsoft MVP award, and a Microsoft Certified Trainer (MCT). Shay often covers PowerShell related topics on his blog and you can also follow him on Twitter at @ShayLevy

Related Posts

%d bloggers like this: