IntelliSense for Parameters (Part 2)

by Feb 14, 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 enumeration types. But what if there is no enumeration type that defines the values you’d like to suggest to the user?

Either define your own enumeration types using the “Enum” keyword (supported in PowerShell 5 or better):

Enum MyVendors
{
    Microsoft
    Amazon
    Google
}

function Get-Vendor 
{
    param(
        [Parameter(Mandatory)]
        [MyVendors]
        $Vendor
    )

    "Chosen vendor: $Vendor"
} 

Or use Add-Type (all PowerShell versions) to create your own enumeration using C# (see below). Note that the C# code is case-sensitive, and the values inside the enumeration are comma-separated. The word after the “enum” keyword defines the type name of the enumeration. Use this word as type for your parameter:

$definition = 'public enum VendorList 
{
  Microsoft,
  Amazon,
  Google
}'
Add-Type -TypeDefinition $definition 


function Get-Vendor 
{
    param(
        [Parameter(Mandatory)]
        [VendorList]
        $Vendor
    )

    "Chosen vendor: $Vendor"
}

Note also that Add-Type cannot edit or overwrite types, so if you want to change the enumeration after you have used it, you either need to restart PowerShell or rename the enumeration. With the newer PowerShell “enum” keyword, you can change the enumeration any time.

Both ways, when the user calls your function and uses the parameter, IntelliSense lists the available values.

 
PS> Get-Vendor -Vendor Amazon
Chosen vendor: Amazon 
 

Attention: when you export your functions to modules, make sure you also add your enumerations to the module. Enumerations must exist before functions are called that use these enumerations.


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!