Site icon TheWindowsUpdate.com

Autocomplete in PowerShell

This post has been republished via RSS; it originally appeared at: ITOps Talk Blog articles.

Autocomplete is something we all like, right? Helps with those moments when we can't quite remember the spelling of a word or the syntax for a command or where we're learning something new and need some help.  I know I have plenty of those moments.

 

PowerShell has some neat, and often underused features that can be enabled to help you have that autocomplete functionality.  You can enable it just for a certain PowerShell session or you can enable it so that every session has it enabled.

 

Tab

The first method I want to show you is to type in a command and then use the Tab button on your keyboard to see what options are available now.  Again, great if you are unsure of what the next command is.

 

To enable this feature for all your PowerShell sessions, to have it load every time you open PowerShell type in

 

Notepad $profile

 

This will open your profile file which you can edit.

 

Within this PowerShell file enter:

 

# Shows navigable menu of all options when hitting Tab Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete

 

And save the file.

 

Now close your PowerShell session and reopen it.

 

Tab completion example

 

Now you are probably saying, Sarah what have you just made me copy and pasted from the Internet?

 

So, the Set-PSReadlineKeyHandler PowerShell command binds keystrokes to functions.  We’ve set the Tab key to show you menu options.  Very simple, yet massively effective.  

 

Note you may have to tweak the PowerShell Execution Policy settings on your machine to make this feature work.  

 

Argument Completer 

The Register-ArgumentCompleter feature is very similar to the method I’ve shown you above, however you can configure it so that the tab completion carries out either a default completion, or built-in cmdlet, or even a custom function.  

 

To fully take advantage of this you need to have some programming skills or some understanding of what PowerShell can do. 

 

However, for those like me that have enough knowledge but not quite enough.  There are folks and teams that have created Register-ArgumentCompleter scripts that you can use. So, for example, one that I have setup in my profile is for the Windows Package Manager tool.  The team have created the argument completer you can add to your profile to help guide you through using the Windows Package Manager tool.  Below is an example of me using it, it’s especially great if you are trying to install a piece of software using Windows Package Manager and are looking for a specific version.  

 

Argument Completer demo

 

The .NET CLI team also have an argument completer available here.

 

There is nothing to say you can't combine these two features together and have them load within your profile every time you launch a PowerShell session, it's what I do.  I want all the things that make my life easier and better loaded each time. 

 

Call to Action 

Do let me know if you have any PowerShell autocomplete tips or suggestions, I'd love to hear them and see what else is out there that can help make life that bit easier! 

Exit mobile version