Skip to the main content.

PowerShell Tips

Using enumerations in PowerShell (2/2)

Table of contents

Post Featured Image

Tobias & Aleksandar's tip #2:

The two very well-known PowerShell experts have teamed up to share their best and most helpful PowerShell tips.

We will be publishing their scripts over the course of Scriptember in 13 blog posts. Don't miss their insights! Be sure to follow all Scriptember events, listed in our calendar here


 

Using enums (part 2)

Simple enums define a list of allowed values, and the user can pick exactly one. When enums are defined as a flag array, then the user can pick more than one value:


[Flags()]
enum Features
{
   BootLog
   RealTimeProtection
   Firewall
   PhysicalAccess
   Leash
}

[Features]$myFeatures = 'Firewall', 'Leash'
$myFeatures 

By adding the [Flags()] attribute to the enum, each enum entry now gets its own unique bit value, thus the user can combine as many enum items as needed. The type [Enum] can shed some light into this. Here it lists the defined enum names:

  • PS C:\> [Enum]::GetNames([Features])


Next, it shows the assigned true numeric values:

  • PS C:\> [Enum]::GetNames([Features]) | ForEach-Object {
       '{0} = {1}' -f $_, ([Features]::$_ -as [int])
    }


When you assign multiple values, bit values are added:

  • PS C:\> [Features]$myFeatures = 'Firewall', 'Leash'
    PS C:\> $myFeatures
    Firewall, Leash 
    PS C:\> $myFeatures.value__


There is just one problem: the assigned numbers are consecutive and thus ambiguous – PhysicalAccess has ID 3, but RealTimeProtection+Firewall also adds to 3.

When you use [Flags()], you also need to manually assign unique values to the enum items:


[Flags()]
enum Features
{
   BootLog =  1
   RealTimeProtection = 2
   Firewall = 4
   PhysicalAccess = 8
   Leash = 16
}


Now each item has a unique ID:

  • PS C:\> [Enum]::GetNames([Features]) | ForEach-Object {'{0} = {1}' -f $_,
    ([Features]::$_ -as [int])}


Now, with unique IDs, you can use enums like any other numbers to do binary arithmetic:

  • PS C:\> [Features]'Bootlog,Leash'
    BootLog, Leash
    PS C:\> [Features]'Bootlog,Leash' -as [int]


Use the "+=" operator to add an element, and "-=" to remove it:

 

  • PS C:\> [Features]$features = 'Bootlog'
    PS C:\> $features += 'Leash'
    PS C:\> $features += 'Firewall'
    PS C:\> $features
    BootLog, Firewall, Leash
    PS C:\> $features -= 'Leash'
    PS C:\> $features
    BootLog, Firewall

Likewise, when testing whether a given value is present in such a list, use binary operators like -band and -bor:

  • PS C:\> $features
    BootLog, Firewall
    PS C:\> ($features -band 'Firewall') -eq 'Firewall'
    True
    PS C:\> ($features -band 'Leash') -eq 'Leash'
    False

     

 

 

Good2know


Scriptember! Stay tuned:

During Scriptember, our calendar provides the best possible overview.  

Scriptember - hier ist die Übersicht

 

  • Follow us on social media, look for and use the hashtags #Scriptember and #PowerShell.
  • Join our live sessions, overview soon here.
  • Participate in challenges and contests. 
  • Share your knowledge.

 

Find all events here!

 

 

Related links 



Related posts

7 min read

Hidden configuration variables in PowerShell

Tobias & Aleksandar's tip #13:

The two very well-known PowerShell experts have teamed up to share their best and most...

4 min read

Hiding confirmations in PowerShell

Tobias & Aleksandar's tip #12:

The two PowerShell experts have teamed up to share their best and most helpful...

4 min read

URL Encoding in PowerShell

Tobias & Aleksandar's tip #11:

The two very well-known PowerShell experts have teamed up to share their best and most...

About the author: