Using a .NET Enumeration for PowerShell Parameter Validation

I recently ran into an issue where I wanted to use the values from a .NET enumeration as the default values for a parameter. That was easy enough:


Although the previous code met my initial requirements, I decided that I also wanted the user to be able to tab expand the values and to validate the values based on the list of colors found in the enumeration without a requirement of having to hard code the values.

My first thought was to use the ValidateSet parameter validation attribute, but it appears that would require static values to be used for the valid colors. Next, I tried using a dynamic parameter which added too much complexity for what I was trying to accomplish:


I discovered that there’s actually an easy way to accomplish this task while eliminating all that unnecessary complexity.

Simply use the class ([System.ConsoleColor] in this example) that describes the .NET enumeration as the parameter data type:

The solution shown in the previous example meets all of my requirements.

It allows all colors in the enumeration to be used as the default value when the Color parameter is not specified without having to hard code the colors:


The valid values (colors in this scenario) which are part of the enumeration can be tab expanded and they show up in intellisense:


If an invalid color which is not a valid color as far as the enumeration is concerned happens to be specified, a meaningful error message is generated which provides the user of the function with a list of valid values:


Reference: Console Class and ConsoleColor Enumeration topics on MSDN.



  1. Jaap Brasser

    Nice reference, good to see your thought process while you were working on this.

  2. crshnbrn66

    This is an awesome article.. Cookie monster has a good article on the same topic both of them are good examples of how to use enum’s


Leave a Reply

%d bloggers like this: