Autocomplete in PowerShell

Published Aug 05 2021 12:01 AM 11.6K Views
Microsoft

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 exampleTab 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 demoArgument 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! 

1 Comment
Microsoft

Also "Ctrl-Spacebar" is by default mapped to "MenuComplete" so if you don't want to edit your defaults, you can still get the menu complete option shown in the above article.

%3CLINGO-SUB%20id%3D%22lingo-sub-2604524%22%20slang%3D%22en-US%22%3EAutocomplete%20in%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2604524%22%20slang%3D%22en-US%22%3E%3CP%3EAutocomplete%20is%20something%20we%20all%20like%2C%20right%3F%20Helps%20with%20those%20moments%20when%20we%20can't%20quite%20remember%20the%20spelling%20of%20a%20word%20or%20the%20syntax%20for%20a%20command%20or%20where%20we're%20learning%20something%20new%20and%20need%20some%20help.%20%26nbsp%3BI%20know%20I%20have%20plenty%20of%20those%20moments.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EPowerShell%20has%20some%20neat%2C%20and%20often%20underused%20features%20that%20can%20be%20enabled%20to%20help%20you%20have%20that%20autocomplete%20functionality.%26nbsp%3B%20You%20can%20enable%20it%20just%20for%20a%20certain%20PowerShell%20session%20or%20you%20can%20enable%20it%20so%20that%20every%20session%20has%20it%20enabled.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH4%20id%3D%22toc-hId-316268314%22%20id%3D%22toc-hId-316268338%22%20id%3D%22toc-hId-316268338%22%20id%3D%22toc-hId-316268338%22%3E%3CSTRONG%3ETab%3C%2FSTRONG%3E%3C%2FH4%3E%0A%3CP%3EThe%20first%20method%20I%20want%20to%20show%20you%20is%20to%20type%20in%20a%20command%20and%20then%20use%20the%20Tab%20button%20on%20your%20keyboard%20to%20see%20what%20options%20are%20available%20now.%26nbsp%3B%20Again%2C%20great%20if%20you%20are%20unsure%20of%20what%20the%20next%20command%20is.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETo%20enable%20this%20feature%20for%20all%20your%20PowerShell%20sessions%2C%20to%20have%20it%20load%20every%20time%20you%20open%20PowerShell%20type%20in%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3ENotepad%20%24profile%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20will%20open%20your%20profile%20file%20which%20you%20can%20edit.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWithin%20this%20PowerShell%20file%20enter%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%23%20Shows%20navigable%20menu%20of%20all%20options%20when%20hitting%20Tab%0A%0ASet-PSReadlineKeyHandler%20-Key%20Tab%20-Function%20MenuComplete%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAnd%20save%20the%20file.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENow%20close%20your%20PowerShell%20session%20and%20reopen%20it.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22tabcomplete.gif%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F300176i115BFA8CD3026894%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22tabcomplete.gif%22%20alt%3D%22Tab%20completion%20example%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3ETab%20completion%20example%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENow%20you%20are%20probably%20saying%2C%20Sarah%20what%20have%20you%20just%20made%20me%20copy%20and%20pasted%20from%20the%20Internet%3F%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESo%2C%20the%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fpowershell%2Fmodule%2Fpsreadline%2Fset-psreadlinekeyhandler%3Fview%3Dpowershell-7.1%26amp%3BWT.mc_id%3Dmodinfra-35653-salean%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3E%3CSTRONG%3ESet-PSReadlineKeyHandler%3C%2FSTRONG%3E%3C%2FA%3E%20PowerShell%20command%20binds%20keystrokes%20to%20functions.%26nbsp%3B%20We%E2%80%99ve%20set%20the%20Tab%20key%20to%20show%20you%20menu%20options.%20%26nbsp%3BVery%20simple%2C%20yet%20massively%20effective.%20%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CEM%3ENote%20you%20may%20have%20to%20tweak%20the%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fpowershell%2Fmodule%2Fmicrosoft.powershell.security%2Fset-executionpolicy%3Fview%3Dpowershell-7.1%26amp%3BWT.mc_id%3Dmodinfra-35653-salean%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3EPowerShell%20Execution%20Policy%3C%2FA%3E%20settings%20on%20your%20machine%20to%20make%20this%20feature%20work.%20%3C%2FEM%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH4%20id%3D%22toc-hId--1491186149%22%20id%3D%22toc-hId--1491186125%22%20id%3D%22toc-hId--1491186125%22%20id%3D%22toc-hId--1491186125%22%3E%3CSTRONG%3EArgument%20Completer%3C%2FSTRONG%3E%3CSTRONG%3E%26nbsp%3B%3C%2FSTRONG%3E%3C%2FH4%3E%0A%3CP%3EThe%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fpowershell%2Fmodule%2Fmicrosoft.powershell.core%2Fregister-argumentcompleter%3Fview%3Dpowershell-7.1%26amp%3BWT.mc_id%3Dmodinfra-35653-salean%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3E%3CSTRONG%3ERegister-ArgumentCompleter%3C%2FSTRONG%3E%3C%2FA%3E%20feature%20is%20very%20similar%20to%20the%20method%20I%E2%80%99ve%20shown%20you%20above%2C%20however%20you%20can%20configure%20it%20so%20that%20the%20tab%20completion%20carries%20out%20either%20a%20default%20completion%2C%20or%20built-in%20cmdlet%2C%20or%20even%20a%20custom%20function.%20%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETo%20fully%20take%20advantage%20of%20this%20you%20need%20to%20have%20some%20programming%20skills%20or%20some%20understanding%20of%20what%20PowerShell%20can%20do.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHowever%2C%20for%20those%20like%20me%20that%20have%20enough%20knowledge%20but%20not%20quite%20enough.%26nbsp%3B%20There%20are%20folks%20and%20teams%20that%20have%20created%20Register-ArgumentCompleter%20scripts%20that%20you%20can%20use.%20So%2C%20for%20example%2C%20one%20that%20I%20have%20setup%20in%20my%20profile%20is%20for%20the%20Windows%20Package%20Manager%20tool.%26nbsp%3B%20The%20team%20have%20created%20the%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fmicrosoft%2Fwinget-cli%2Fblob%2F1fbfacc13950de8a17875d40a8beb99fc6ada6c2%2Fdoc%2FCompletion.md%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Eargument%20completer%3C%2FA%3E%20you%20can%20add%20to%20your%20profile%20to%20help%20guide%20you%20through%20using%20the%20Windows%20Package%20Manager%20tool.%20%26nbsp%3BBelow%20is%20an%20example%20of%20me%20using%20it%2C%20it%E2%80%99s%20especially%20great%20if%20you%20are%20trying%20to%20install%20a%20piece%20of%20software%20using%20Windows%20Package%20Manager%20and%20are%20looking%20for%20a%20specific%20version.%26nbsp%3B%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22argumentcompleter.gif%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F300173iDAC86A1EA5FA7004%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22argumentcompleter.gif%22%20alt%3D%22Argument%20Completer%20demo%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EArgument%20Completer%20demo%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20.NET%20CLI%20team%20also%20have%20an%20argument%20completer%20available%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fdotnet%2Fcore%2Ftools%2Fenable-tab-autocomplete%3FWT.mc_id%3Dmodinfra-35653-salean%23powershell%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehere%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThere%20is%20nothing%20to%20say%20you%20can't%20combine%20these%20two%20features%20together%20and%20have%20them%20load%20within%20your%20profile%20every%20time%20you%20launch%20a%20PowerShell%20session%2C%20it's%20what%20I%20do.%26nbsp%3B%20I%20want%20all%20the%20things%20that%20make%20my%20life%20easier%20and%20better%20loaded%20each%20time.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH4%20id%3D%22toc-hId-996326684%22%20id%3D%22toc-hId-996326708%22%20id%3D%22toc-hId-996326708%22%20id%3D%22toc-hId-996326708%22%3ECall%20to%20Action%26nbsp%3B%3C%2FH4%3E%0A%3CP%3EDo%20let%20me%20know%20if%20you%20have%20any%20PowerShell%20autocomplete%20tips%20or%20suggestions%2C%20I'd%20love%20to%20hear%20them%20and%20see%20what%20else%20is%20out%20there%20that%20can%20help%20make%20life%20that%20bit%20easier!%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-2604524%22%20slang%3D%22en-US%22%3E%3CP%3ELet's%20take%20a%20look%20at%20how%20you%20can%20configure%20your%20PowerShell%20profile%20to%20help%20you%20autocomplete%20commands!%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22powershellautocomplete.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F300237i16A890AF17D7D409%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22powershellautocomplete.png%22%20alt%3D%22powershellautocomplete.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2604524%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESarah%20Lean%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2660165%22%20slang%3D%22en-US%22%3ERe%3A%20Autocomplete%20in%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2660165%22%20slang%3D%22en-US%22%3E%3CP%3EAlso%20%22Ctrl-Spacebar%22%20is%20by%20default%20mapped%20to%20%22MenuComplete%22%20so%20if%20you%20don't%20want%20to%20edit%20your%20defaults%2C%20you%20can%20still%20get%20the%20menu%20complete%20option%20shown%20in%20the%20above%20article.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Co-Authors
Version history
Last update:
‎Aug 03 2021 10:04 AM
Updated by: