This PowerShell blog post defines how to do this in PowerShell 1.0. In PowerShell 2.0, you can use Add-Type as follows:
C:\PS> Add-Type -TypeDefinition @' >> public enum MyEnum { >> A, >> B, >> C, >> D >> } >> '@ >>
Update: Here's how to use the enumeration:
C:\PS> function foo([MyEnum]$enum) { $enum } C:\PS> foo ([MyEnum]::A) A
You need parentheses around the argument to parse the argument as a type. This is necessary because arguments are treated more or less like strings. Knowing this, you can also pass them enum in simple string form, and powershell will figure it out:
C:\PS> foo A A C:\PS> $arg = "B" C:\PS> foo $arg B C:\PS> foo F error*
error - F is not one of the listed values ββ- valid values ββinclude A, B, C, D *
Keith Hill Sep 17 '10 at 14:35 2010-09-17 14:35
source share