Using Enumerations

by Jun 21, 2016

Beginning with PowerShell 5.0, you can use the new keyword "enum" to create enumerations. They can be useful for parameter validation:

#requires -Version 5
enum MyFavoriteCities 
{
  Hannover
  Seattle
  London
  NewYork
}


function Select-City
{
  param
  (
    [MyFavoriteCities]
    [Parameter(Mandatory=$true)]
    $City
  )
  
  "You chose $City"
}

The -City parameter now only accepts the values listed in the enumeration, and in the PowerShell ISE, you even get intelliSense.

However, for this to work the enum needs to be defined before you run the function.

Twitter This Tip! ReTweet this Tip!