Home HowTo Parameters Finding Public Enums For Parameter AutoCompletion
 

Finding Public Enums For Parameter AutoCompletion

Make sure your parameters provide autocompletion

one line of code is enough for your function parameters to provide rich autocompletion

PowerShell 3.0 finally auto-completes parameter values, but it needs to know what values to expect. That’s easy by assigning an enumeration data type to your parameter. What’s not so easy is to know which enumeration types are available. That’s why we built a little spy function that finds the types you need.

One Line Of Code Adds AutoCompletion

Take a look at how simple it is in PowerShell 3.0 to add rich autocompletion to your function parameters! Here is a function with one parameter. This parameter asks the user for a network interface type. PowerShell automatically provides autocompletion, and ISE shows Intellisense menus without you having to define what network interface types are.


function Test-Completion
{
    param
    (
        
        $Value
    )

    “You chose: $Value
}

The miracle happens in your parameter declaration. You simply assign the data type [System.Net.NetworkInformation.NetworkInterfaceType] to it. PowerShell does the rest. Isn’t that beautiful? What’s not so beautiful is: how would you know that there is such a type? And what other types are available for other parameter values?

Finding System Enumerations

PowerShell is all about code reuse and not having to do the same work over an over again. Which is why you do not need to put together lists for common items (such as the network interface types). That work has been done for you inside the .NET framework, and there are lists for almost anything. These lists are called “enumerations“. They are data types with a sole purpose: defining legal values. To find these values, you can search the .NET framework, and we created a function called Find-Enum that makes it a snap. All you need to do is specify what you are looking for.
Finding enumeration data types

let PowerShell search for keywords and return all suitable enumeration data types

Simply submit a keyword (use wildcards if you want to widen your search). Here are some samples:
PS> Find-Enum red

Name                                                         Values                                                       Source                                                     
                                                                                                                                                                     
[System.ConsoleColor]                                        Black, DarkBlue, DarkGreen, DarkCyan, DarkRed, DarkMagent… CommonLanguageRuntimeLibrary                               

PS> Find-Enum red -All

Name                                                         Values                                                       Source                                                     
                                                                                                                                                                     
[System.ConsoleColor]                                        Black, DarkBlue, DarkGreen, DarkCyan, DarkRed, DarkMagent… CommonLanguageRuntimeLibrary                               
[System.Drawing.KnownColor]                                  ActiveBorder, ActiveCaption, ActiveCaptionText, AppWorksp… System.Drawing.dll                                         

PS> Find-Enum desktop

Name                                                         Values                                                       Source                                                     
                                                                                                                                                                     
[System.Environment+SpecialFolder]                           Desktop, Programs, MyDocuments, Personal, Favorites, Star… CommonLanguageRuntimeLibrary                               
[Microsoft.Win32.UserPreferenceCategory]                     Accessibility, Color, Desktop, General, Icon, Keyboard, M… System.dll                                                 

PS> Find-Enum user

Name                                                         Values                                                       Source                                                     
                                                                                                                                                                     
[System.EnvironmentVariableTarget]                           Process, User, Machine                                       CommonLanguageRuntimeLibrary                               
[System.IO.IsolatedStorage.IsolatedStorageScope]             None, User, Domain, Assembly, Roaming, Machine, Application  CommonLanguageRuntimeLibrary                               
[System.Security.PolicyLevelType]                            User, Machine, Enterprise, AppDomain                         CommonLanguageRuntimeLibrary                               
[System.Security.Principal.WindowsBuiltInRole]               Administrator, User, Guest, PowerUser, AccountOperator, S… CommonLanguageRuntimeLibrary
Find-Enum by default returns all enumerations in the default assemblies. It is smart enough to exclude assemblies that are not part of PowerShell, so you are not running the risk of finding enumerations that aren’t generally available, breaking your script once you run it somewhere else. If you do want to search all loaded assemblies, add -All to the call. Now the results may include types that are not included in every PowerShell. For example, if you run Find-Enum in the ISE editor and use -All, you will also get back enumeration types from the libraries that ISE uses to display its windows. Once you identified an enumeration type that lists the values you want for your parameter, simply copy and paste the type name in the name column, then assign this type to your function parameter – Done!

Find-Enum: Your Search Tool

As always, you can easily download the script with the Find-Enum function here:

Finds enumeration datatypes by keyword
Finds enumeration datatypes by keyword
find-enum.ps1
Version: 1.0
2.0 KiB
669 Downloads
Details...
And here’s the source code. It is not much. Find-Enum gets all loaded assemblies, excludes the non-default ones, then searches for enumeration types and filters for the names you are after:

<#  .SYNOPSIS
  Searches assemblies for enumeration data types that can be used to declare PowerShell function parameters
  .DESCRIPTION
  By default, only default assemblies are searched that are generally available in PowerShell. 
  With the switch parameter -All, all currently loaded assemblies are searched.
  The results may then include types that are not commonly available and may require modules or assemblies to be loaded first.
  .EXAMPLE
  Find-Enum
  lists all enumeration data types for all default assemblies
  .EXAMPLE
  Find-Enum *comput*
  lists all enumeration data types for all default assemblies that contain at least one value that matches “*comput*
  .EXAMPLE
  Find-Enum red -All
  finds all enumeration data types in all currently loaded assemblies that contain the value “red”
#>

function Find-Enum
{
    param
    (
        $Value = ‘*’,
        $Name = ‘*’,
        
        $All
    )

    $default = ‘CommonLanguageRuntimeLibrary’, ‘Microsoft.CSharp.dll’, ‘Microsoft.Management.Infrastructure.dll’, ‘Microsoft.PowerShell.Commands.Management.dll’, 
    ‘Microsoft.PowerShell.Commands.Utility.dll’, ‘System.Configuration.dll’, ‘System.Configuration.Install.dll’, ‘System.Core.dll’, 
    ‘System.Data.dll’, ‘System.DirectoryServices.dll’, ‘System.dll’, ‘System.Management.Automation.dll’, ‘System.Management.dll’, 
    ‘System.Transactions.dll’, ‘System.Xml.dll’

::CurrentDomain.GetAssemblies() | 
  Where-Object { $All -or ($default -contains $_.ManifestModule) } |
  ForEach-Object { try { $_.GetExportedTypes() } catch {} } | 
  Where-Object { $_.isEnum } |
  Where-Object { $_.Name -like $Name } |
  Sort-Object -Property Name |
  ForEach-Object {

    $rv = $_ | Select-Object -Property Name, Values, Source
    $rv.Name = ‘[{0}]’ -f $_.FullName
    $rv.Source = $_.Module.ScopeName
    $rv.Values = ::GetNames($_) -join ‘, ‘
    $rv
  } |
  Where-Object { @($_.Values -split ‘, ‘) -like $Value } 
}
PSv3

Finds enumeration datatypes by keyword
Finds enumeration datatypes by keyword
find-enum.ps1
Version: 1.0
2.0 KiB
669 Downloads
Details...