Understanding Classes in PowerShell – Part 03 – Adding Enums

This blog post is continuation of series of posts on understanding classes in PowerShell. Part 02 of series can be read at here.

Adding Enums to PowerShell Classes

What are Enums

Sometimes, it makes sense to use Enums. After all, there can be so many variations of a particular property and coding them in advance helps in automatic parameter validation. When applied as a type constraint for a property in a Windows PowerShell class, an enum will generate an error if the value supplied is not a valid enumeration.

For example, let’s consider a property called color. If we stick to basic color such as red or blue, it is great. We can limit our search by constraint such as red and it will return all matching results. But if we use Scarlet, imperial red, indian red, spanish red, rusty red, ruby, chili red, redwood and so on, it can get messy quickly. 

How to use Enums

We can define Enums in PowerShell by using keyword Enum. For example,

Enum ColorOfCar
{
    Red = 1
    Blue = 2
    Green = 3
}

We can then access each property as a static property. This is shown here:

PS C:\Users\mredw> [Color]::Blue
Blue

And, if we want the value associated with the property, we can get it by adding .value_:

PS C:\Windows\system32> [Color]::Blue.value__
2

Using Enums with Classes

To use enums with classes, first we need to define enums. After that, we can use it as a type/constraint for the property in my class property section. For example, let’s consider below script:

Enum Color
{
 Red = 1
 Blue = 2
 Green = 3
}

Class Paint{
 [String]$Make
 [Color]$color
}

$newPaint = [Paint]::new()
$newPaint.color = 1
$newPaint.Make = "Nerolac"
$newPaint

This will generate an output like below:

PS C:\Windows\system32> $newPaint

Make color
---- -----
Nerolac Red

We can also use noun of the enum property also. For example, $newPaint.color = "Red" would also work fine.

The benefit of using enums is now if someone tries to assign a value which is outside of the defined properties in enum, it will fail. For example, using $newPaint.color = "crimson red" will fail with below error:

PS C:\Windows\system32> $newPaint.color = "Crimson Red"
Exception setting "color": "Cannot convert value "Crimson Red" to type "Color". Error: "Unable to match the identifier name Crimson Red to a valid enumerator name. Specify one of 
the following enumerator names and try again:
Red, Blue, Green""
At line:1 char:1
+ $newPaint.color = "Crimson Red"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], SetValueInvocationException
    + FullyQualifiedErrorId : ExceptionWhenSetting

That’s all to using enums with PowerShell classes.

One thought on “Understanding Classes in PowerShell – Part 03 – Adding Enums

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s