3

#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()
Filed in: Columns, Tips and Tricks Tags: , , , ,

3 Responses to "#PSTip Getting Enum values in PowerShell 3.0"

Leave a Reply

Submit Comment

© 2016 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: