PowerShell Parameter Value Tab Completion in Version 3


I have been playing with PowerShell Version 3 more and more, and one of the first features that caught my eye was tab completion for values of a parameter. Tab completion to find available parameters has always been fantastic, but having the ability to tab through parameter VALUES is simply awesome.

To see it work with out of the box cmdlets, launch PowerShell version 3 and try one of the following two examples:

Get-Process -Name {tab}
Write-Host "Test" -ForegroundColor {tab}

In example 1, you should see Process names populating as you tab through.
In example 2, you should see available foreground colors (Black, Blue, Cyan, etc.)

Not only is this fantastic behavior out of the box, it is really easy to use this in custom scripts or functions.

I have created a sample function that I will share below, but essentially you just need to add a ValidateSet attribute on each parameter where you will have specific available values. The code for that looks like this:

[ValidateSet("Red","Green","Blue")]

And here is the full sample function:

function Test-V3Function {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)]
[ValidateSet("Red","Green","Blue")]
$Color
)
Write-Host "You selected $($Color)"
}

After you have dot-sourced this function, if you run the following you will be able to tab through to Red, Green and Blue:

Test-V3Function -Color {tab}

Sweet!!

Advertisements

One thought on “PowerShell Parameter Value Tab Completion in Version 3

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s