#PSTip Getting Enum values in PowerShell 3.0

Note: This tip requires PowerShell 3.0 or above.

Prior to PowerShell 3.0, to return a list of  names or values of an Enumeration object, we needed to use the static methods of the System.Enum type:

PS> [System.Enum]::GetNames('System.ConsoleColor')
Black
DarkBlue
DarkGreen
DarkCyan
(...)

PS> [System.Enum]::GetValues('System.ConsoleColor')
Black
DarkBlue
DarkGreen
DarkCyan
(...)

PowerShell 3.0 runs on .NET 4.0 and in .NET 4.0 we can get the same information using new System.Type methods:

[System.ConsoleColor].GetEnumValues()

- or -

[System.ConsoleColor].GetEnumNames()

About the author: Shay Levy

Shay Levy is a Co-founder and editor of the PowerShell Magazine. He is a multiple-year recipient of the Microsoft MVP award, and a Microsoft Certified Trainer (MCT). Shay often covers PowerShell related topics on his blog and you can also follow him on Twitter at @ShayLevy

Related Posts

%d bloggers like this: