IntelliSense for Parameters (Part 3)

by Feb 18, 2020

Wouldn’t it be nice if parameters would suggest valid arguments for the user? Sometimes they do. When you type below command and press a SPACE after -LogName, PowerShell ISE and Visual Studio Code pop up an IntelliSense menu with all log files you can dump:

 
PS> Get-EventLog -LogName  
 

If no automatic IntelliSense pops up (i.e. in the PowerShell console), you can press TAB for auto completion, or CTRL+SPACE to manually force the IntelliSense choices to appear.

You can do the same with your own PowerShell functions, and there are a number of ways to do this. In our last part we looked at using custom enumeration types. Today, let’s look at easier ways (and some hidden tricks tied to it).

Probably the easiest way to provide parameters with IntelliSense is the use of the attribute “ValidateSet”: you simply qualify the parameter with a list of allowable values:

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

    "Chosen vendor: $Vendor"
}

The $vendor variable is of type “string”, but internally PowerShell makes sure that only the values listed in “ValidateSet” are assignable. You can use the same trick for regular variables as well, and add an extra level of security to your code:

[ValidateSet('dc1','dc2','ms01')]$servers = 'dc1'

# works
$servers = 'dc2'

# fails
$servers = 'dc3'

And here’s another trick: the “ValidateSet” attribute only applies to variable and parameter assignments, but it does not apply to parameter default values. You as function author can assign a default value to the parameter that is not in the list of user-assignable values:

function Get-Vendor {
    param(
        [ValidateSet('Microsoft','Amazon','Google')]
        [string]
        $Vendor = 'Undefined'
    )

    "Chosen vendor: $Vendor"
} 

When the user calls Get-Vendor without arguments, $vendor is set to “Undefined”. Once the user assigns a value to the parameter, this value is not available, easily helping you to distinguish whether the user made a selection or not.


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!