PowerShell autocomplete and Windows Package Manager

WinGet Jul 13, 2021

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

I've been really enjoying learning about Windows Package Manager lately and realised the team have pulled together a PowerShell function using Register-ArgumentCompleter.

Setup Completion

To make use of the auto completion functionality we need to configure our PowerShell environment first.  You can either run the command to use it within your current session or you can configure your PowerShell Profile to load it every time you use PowerShell.

To modify my profile I type in the command

notepad $PROFILE

This will launch a notepad editor opening my PowerShell profile file.  In this file I want to add the PowerShell Command:

Register-ArgumentCompleter -Native -CommandName winget -ScriptBlock {
    param($wordToComplete, $commandAst, $cursorPosition)
        [Console]::InputEncoding = [Console]::OutputEncoding = $OutputEncoding = [System.Text.Utf8Encoding]::new()
        $Local:word = $wordToComplete.Replace('"', '""')
        $Local:ast = $commandAst.ToString().Replace('"', '""')
        winget complete --word="$Local:word" --commandline "$Local:ast" --position $cursorPosition | ForEach-Object {
            [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
        }
}

I save the file and close it.

Configure PowerShell profile

Using Completion with Windows Package Manager

Now that we have the completion settings configured let's start to use it with Windows Package Manager!

If we start to type the command to trigger Windows Package Manager we can start to use the tab key to complete the command line we need.

So we start by typing winget then we can tab. Install is the first command so we will see that, which is the one we want in this example.

We then want to look for PowerToys so we start typing and find the PowerToys Preview software available to install, by tabbing through the options shown to us.

Now we want to look at versions, so we can start to type to find that switch within in the Windows Package Manager syntax.  The auto completion helps us find the version switch.

We can now hit tab and it will show us all the versions of PowerToys Preview that is available to install.

You can see this in action below:

Or it would look like this in terms of inputs into the keyboard:

Input Result
winget ⇥ winget install
winget install power⇥ winget install "PowerToys (Preview)"
winget install "PowerToys (Preview)" --⇥ winget install "PowerToys (Preview)" --version
winget install "PowerToys (Preview)" --version⇥ winget install "PowerToys (Preview)" --version 0.41.4

Give it a go and let me know how you get on with it! 👌

Tags

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.