#PSTip Displaying all possible combinations of a flagged enumeration

In the last tip, I showed you how to create an enumeration object on which we can do bitwise operations to derive combinational values. Now, in this tip, let us see how to retrieve all possible combinations.

Assuming that we forgot the values assigned to each enumeration constant, let us first see how to see the values of each constant:

[Enum]::GetValues([Scripting.Skill]) | % { "{0,3} {1}" -f $([int]$_),$_ }

So, this gives us something similar to:

Since the total of all these values is 31, let us use how to derive all possible combinations.

for ($i = 0; $i -lt 31; $i++) {
   "{0,3} {1}" -f $([int]$i), [Scripting.Skill]$([int]$i)
}

This output is generally longer! So, try it yourself!

About the author: Ravikanth C

Ravikanth is the founder and editor of the PowerShell Magazine. He is also a PowerShell MVP who works at Dell Inc. He blogs at http://www.ravichaganti.com/blog and you can follow him on Twitter @ravikanth.

Related Posts

%d bloggers like this: