Main | Contact | Blog | Documentation

PowerShell Framework

The project dedicated to empowering your PowerShell scripting.

Getting started with Tab Completion

Tab Completion is what happens in PowerShell when you hit the TAB key:

This third option isn’t always so simple, because PowerShell needs to know what to offer and it is not always able to do so. When it can’t, it generally offers files and folders in the current folder.

Instead, you can tell PowerShell what to do.

Command to complete

First of all, we need a command PowerShell can complete for:

function Get-Alcohol
{
    [CmdletBinding()]
    Param (
        [string]
        $Type,

        [string]
        $Unit = "Pitcher"
    )

    Write-Host "Drinking a $Unit of $Type"
}

Now let’s add tab completion for it!

Completing a parameter

In Step one, we create a scriptblock that creates strings and register it under a name:

# Create scriptblock that collects information and name it
Register-PSFTeppScriptblock -Name "alcohol" -ScriptBlock { 'Beer','Mead','Whiskey','Wine','Vodka','Rum (3y)', 'Rum (5y)', 'Rum (7y)' }

This allows us to assign that scriptblock to a command and parameter:

# Assign scriptblock to function
Register-PSFTeppArgumentCompleter -Command Get-Alcohol -Parameter Type -Name alcohol

Try it out:

Get-Alcohol -Type <TAB>

Additional Information

Further reading