Tag Archives: [System.Enum]

QuickTip: Enumerate objects with [System.Enum] class

In PowerShell it can be useful to be working directly with .Net classes and runspaces. The disadvantage is that Get-Help is not available to help you explore the possible values that can be entered. Although MSDN is a great reference for .Net runspaces and classes it can be very useful to know how to get the values of an enumeration without accessing MSDN.

For example when capturing a single key press the $host variable can be used, specifically the ReadKey method. In order to display the overload definitions I will execute the method without the including the parentheses brackets:

PS> $host.UI.RawUI.ReadKey
System.Management.Automation.Host.KeyInfo ReadKey(System.Management.Automation.Host.ReadKeyOptions options)
System.Management.Automation.Host.KeyInfo ReadKey()

So now it would be good to know what the possible values for System.Management.Automation.Host.ReadKeyOptions enumeration are. To find out what the possible values are the GetNames method of the [System.Enum] Class can be used in the following way:

PS> [System.Enum]::GetNames([System.Management.Automation.Host.ReadKeyOptions])

Knowing how to find information like this in PowerShell provides you with the tools to be able to troubleshoot and improve your scripts. In the next Quicktip on Thursday I will show how the $host.UI.RawUI.ReadKey method can be used to get grab a single key press from the PowerShell console.