August 27th, 2015

Working with Enums in PowerShell 5

Doctor Scripto
Scripter

Summary: Ed Wilson, Microsoft Scripting Guy, talks about working with enums in Windows PowerShell 5.0 in Windows 10.

Microsoft Scripting Guy, Ed Wilson, is here. Yesterday, I talked about the new stealth feature in Windows PowerShell 5.0 in Windows 10 that permits me to easily create an enum. Enums are great because they provide a concise way for me to check parameters, a great way to make code easier to read, and many other things.

Note  Today's post continues New PowerShell 5 Feature: Enumerations. You should read it prior to reading today's post.

Awhile back, I wrote a function called Get-EnumValues, which has since come to reside in my Windows PowerShell profile. I talk about this function in One of My Favorite PowerShell Functions. What is cool about the function is that it accepts an enum, and then it returns an array of the enumeration names and the associated numeric values.

This is an extremely powerful and helpful tool to have around when one is working with enums (whether self-created or one of the thousands of enums that reside in the .NET Framework). 

Here is the function:

Function get-enumValues

{

 # get-enumValues -enum "System.Diagnostics.Eventing.Reader.StandardEventLevel"

Param([string]$enum)

$enumValues = @{}

[enum]::getvalues([type]$enum) |

ForEach-Object { 

$enumValues.add($_, $_.value__)

}

$enumValues

}

To use the function, I simply call Get-EnumValues and specify the name of the enum in quotation marks:

PS C:\Users\mredw> get-enumValues -enum "fruit"

Name                     Value                                                   

—-                           —–                                                   

Kiwi                           31                                                      

Pear                         30                                                       

Apple                        29    

One of the cool things I can use is the [enum] type accelerator, and I can use that to display the values that are defined inside an enum. This is shown here:

PS C:\Users\mredw> [enum]::GetValues([type]"fruit")

Apple

Pear

Kiwi

But it does not do what I would expect. I would expect to see the numeric values, not the enumeration names or properties. As shown here, when I reference a property via the enumeration, it simply displays the name again:

PS C:\Users\mredw> [fruit]::Apple

Apple

The secret is knowing that there is a value__ property associated with each enumeration. When I call that, then I get the numeric value:

PS C:\Users\mredw> [fruit]::Apple.value__

29

That is the “trick” that my Get-EnumValues function uses to create the hash table it displays.

Create new enum based on existing enum

One of the really cool things I can do with enums is use them to create new enums. To continue my example from yesterday, I can create more fruit. My original enum is shown here:

Enum Fruit

{

 Apple = 29

 Pear = 30

 Kiwi = 31

}

Now I decide that I want to create more fruit. So I call my new enum MoreFruit:

Enum MoreFruit

{

}

To combine a pear and an apple, I get a Papple. It is as simple as calling the pear enumeration and the apple enumeration and adding them together. This is shown here:

Papple = [fruit]::Pear + [fruit]::Apple

I can also combine a kiwi and an apple and get a Kapple:

Kapple = [fruit]::Kiwi + [fruit]::Apple

I imagine that I could combine a kiwi, a pear, and an apple and get a KaPapple, and this is what I do:

KaPapple = [fruit]::Kiwi + [fruit]::Pear + [fruit]::Apple

The complete script is shown here:

Enum Fruit

{

 Apple = 29

 Pear = 30

 Kiwi = 31

}

Enum MoreFruit

{

 Papple = [fruit]::Pear + [fruit]::Apple

 Kapple = [fruit]::Kiwi + [fruit]::Apple

 KaPapple = [fruit]::Kiwi + [fruit]::Pear + [fruit]::Apple

}

When I run it, I can access the KaPapple from my MoreFruit enum:

PS C:\Users\mredw> [morefruit]::KaPapple

KaPapple

I can also see the numeric value of the KaPapple:

PS C:\Users\mredw> [morefruit]::KaPapple.value__

90

I see that it added 29, 30, and 31 together to get 90. I can now use my Get-EnumValues function to see what else is in my MoreFruit enum. The output is shown here:

Image of command output

That is all there is to using enums. Join me tomorrow when I will talk about more cool stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

0 comments

Discussion are closed.