Finding Enumeration Data Types

by Nov 19, 2012

In a previous tip you learned that assigning an enumeration data type to a function parameter automatically enables argument completion in PowerShell 3.0. This just takes one line of code, but the hard part is to find a suitable enumeration data type that has a list of values that matches what you need.

Here is some code that'll find and list all enumeration data types available in your PowerShell session:

[AppDomain]::CurrentDomain.GetAssemblies() |
  ForEach-Object { $_.GetExportedTypes() } |
  Where-Object { $_.isEnum } |
  Sort-Object FullName |
  ForEach-Object {
    $values = [System.Enum]::GetNames($_) -join ','
    $rv = $_ | Select-Object -Property FullName, Values
    $rv.Values = $values
    $rv
  }  

When you run this, you may get some error messages that you can safely ignore. You do get a list of enumeration types plus a list of the values they represent.

Twitter This Tip! ReTweet this Tip!