New PowerShell 5 Feature: Enumerations


Summary: Ed Wilson, Microsoft Scripting Guy, talks about creating enumerations in Windows PowerShell 5.0.

Microsoft Scripting Guy, Ed Wilson, is here. It is nearly a stealth feature in Windows PowerShell 5.0—there is an Enum keyword. Yep, that's right, there is an Enum keyword. I wonder what it does? Probably has something to do with creating enums.

Is this a big deal? You betcha...as they may say in some places.

Why? Because prior to Windows PowerShell 5.0, you had to basically create an enum by using C# kinds of code, and then adding it as a type. Did it work? Sure it did. Was it Windows PowerShell? Well, in that it actually worked and I wrote it in Windows PowerShell—sure it was Windows PowerShell.

Note  For reference, refer to my Hey, Scripting Guy! Blog post The Fruity Bouquet of Windows PowerShell Enumerations.

Before PowerShell 5.0

Before Windows PowerShell 5.0, if I wanted to create an enumeration, I basically wrote inline C# code. Here is an example that creates a simple enumeration that assigns numeric values to three different types of fruit:

# Create-FruitEnum.ps1

$enum = "

namespace myspace

{

public enum fruit

{

apple = 29, pear = 30, kiwi = 31

}

}

"

Add-Type -TypeDefinition $enum -Language CSharpVersion3

This is pretty cool code, and the fact that I could create a custom enum in a scripting language was pretty wild. I wrote this code in 2010, and it has worked ever since. In fact, nothing has improved in this regards in the past five years–—until Windows PowerShell 5.0 came out in Windows 10.

Use the enum keyword

So, whereas I used to have to create a giant string, with embedded C# code, and then use the Add-Type command to add in C# code, now I have an enum keyword. This makes the code much cleaner, and very easy to use.

I use the enum keyword and assign a name for the enum. I then open and close a script block. This appears here:

Enum Fruit

{

}

Now I simply assign numeric values for each property:

Enum Fruit

{

 Apple = 29

 Pear = 30

 Kiwi = 31

}

When I run my script, it creates the enum. I can then access each property as a static property. This is shown here:

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

Apple

The cool thing is that IntelliSense pops up and shows the permissible members:

That is all there is to using enumerations in Windows PowerShell 5.0.  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 

Comments (4)

  1. Sami Koskivaara says:

    Do you think this is a bug or a desired functionality:
    https://connect.microsoft.com/PowerShell/feedback/details/1654123/powershell-5-enum-definitions

    Basically you can explicitly create a specific enum type variable [fruit]$fruit but you can assign a value to that from another enum type $fruit = [AnotherEnum]::RandomValue

  2. Sami,

    The default underlying type of enumeration elements is int. For example if you take your example provided with pizza and sizes you can see that [Size]::Large.value__ is actually [int]2. Since [Size]::Large.value__ will be resolved to 2 it will actually look
    like this: [PizzaType]$a = 2 which will finally return you value of "Peperoni".

    Taking into the account Powershell ETS, this looks like "desired functionality".

    Ivan

  3. SamiKoskivaara says:

    It’s very inconsistent because you can’t mix different classes:

    Class Fruit {
    [string]$name
    }

    Class Car {
    [string]$name
    }

    [Fruit]$fruit = [Car]::New()
    ERROR: Cannot convert the "Car" value of type "Car" to type "Fruit".

    And the type is really an Enum type with name "Size", it’s not an int.
    $a = [Size]::Small
    $a.GetType()

    Name BaseType
    —- ——–
    Size System.Enu

  4. Sami,

    I didn’t say that Enum is int. Enum element value is typeof int. Check what $a.value__ will return. And example provided in previous comment is based on classes, not on enums so it is not relevant. Please check msdn enum reference.

    Ivan

Skip to main content