Autocomplete in PowerShell
Published Aug 05 2021 12:01 AM 265K 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! 

5 Comments
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.

Copper Contributor

After executing the very first command in PS, i.e., 

 

Notepad $profile

 

a pop-up on my Notepad shows an error - "The system cannot find the path specified"
How do I solve this problem?
Thanks.

Copper Contributor

@ONLYZEUS

That means you don't have a PowerShell profile file yet, you must create one before trying to edit it

Just type

New-Item $PROFILE

 into PowerShell to automatically create a new file with the correct path and extension

 

Then, open it with notepad like the post said:

notepad $PROFILE

 

You could of course use any editor you want.

Note: The preview Notepad version that's coming sometime soon actually asks you if you want to create a file that doesn't exist yet - so there's a good chance that soon you wouldn't encounter that error anyway.

Copper Contributor

Screenshot (5).png

@AdamWojcik  What do I do now? 
I'm sorry sir but  I'm a newbie and exploring powershell, so you have to tell me assuming I'm a complete ameture. 

 

Copper Contributor

@ONLYZEUSdo

 

mkdir C:\Users\Hp\Documents\WindowsPowerShell
new-item C:\Users\Hp\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

 

then do run "notepad $profile" again.

Version history
Last update:
‎Nov 09 2023 11:09 AM
Updated by: