Secret Dynamic Argument Completer

by Feb 28, 2020

In the previous tip we introduced the lesser-known “ArgumentCompletion” attribute that can provide IntelliSense-like autocompletion to parameters. This attribute can do way more than that, though. Previously, we introduced this code:

function Get-Vendor {
    param(
        [Parameter(Mandatory)]
        [ArgumentCompleter({'Microsoft','Amazon','Google'})]
        [string]
        $Vendor
    )

    "Chosen vendor: $Vendor"
}

When a user called Get-Vendor, by pressing TAB or CTRL-SHIFT the suggestions listed in the “ArgumentCompleter” attribute would appear.

You may have wondered why the list of strings is embedded inside curly brackets (a script block). The answer is: because this piece of code gets actually executed when the user invokes completion. You can dynamically compute the completion texts as well.

The Submit-Date function has one parameter called -Date. The autocompleter completes the current date and time in quoted ISO format whenever you press TAB:

function Submit-Date {
    param(
        [Parameter(Mandatory)]
        [ArgumentCompleter({ '"{0}"' -f (Get-Date -Format "yyyy-MM-dd HH:mm:ss") })]
        [DateTime]
        $Date
    )

    "Chosen date: $Date"
}  

Try it by running the code. Next, enter Submit-Date, a space, then press TAB.

           	
PS> Submit-Date -Date "2020-01-21 16:33:19"

Likewise, the next function implements the -FileName parameter, and when you press TAB, it auto-completes with the actual file names inside your Windows folder:

function Get-File {
    param(
        [Parameter(Mandatory)]
        [ArgumentCompleter({Get-ChildItem -Path $env:windir -Name})]
        [string]
        $FileName
    )

    "Chosen file name: $FileName"
}

Get-File -FileName

Any time the user invokes autocompletion by pressing TAB or CTRL-SPACE, the code in the script block submitted to ArgumentCompleter executes, and the result is used for auto-completion.

That’s probably why the AutoCompleter attribute does not pop up IntelliSense automatically but only on user request. Note that autocompletion may not work in editor panes. It is designed for the interactive PowerShell console.

 


You are a PowerShell Professional, passionate about improving your code and skills? You take security seriously and are always looking for the latest advice and guidance to make your code more secure and faster? You’d love to connect to the vibrant PowerShell community and get in touch with other PowerShell Professionals to share tricks and experience? Then PowerShell Conference EU 2020 might be just the right place for you: https://psconf.eu (June 2-5, 2020 in Hanover, Germany).

It’s a unique mixture of classic conference with three parallel tracks filled with fast-paced PowerShell presentations, and advanced learning class with live discussions, Q&A and plenty of networking.

Secure your seat while they last: https://psconf.eu/register.html. The speakers and agenda is available here: https://psconf.eu/schedule.

Twitter This Tip! ReTweet this Tip!