Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

Get PowerShell Cmdlet Parameter Enum Values Easily

Enumeration is the action of mentioning a number of things one by one, often to count them or describe them but it has specific meanings in computer science, data types like enum for named constants. 

In the PowerShell to list the enumeration information for a parameter, you will need to use elementory .NET Framework type of the parameter and then use the [System.Enum]::GetNames() static method. In the step one you need to fist identify the Parameter type to retrieve the Parameter Type. In the below example I will use Get-Command cmdlet and use common parameter ErrorAction to know the all the enum values. As it can be seen its BaseType is System.Enum.

(Get-Command -Name Get-Service).Parameters.ErrorAction.ParameterType

IsPublic   IsSerial  Name             BaseType
--------   --------  ----             --------
True       True      ActionPreference System.Enum
After furthermore digging and running below command it shows output System.Management.Automation.ActionPreference on the console
(Get-Command -Name Get-Service).Parameters.ErrorAction.ParameterType.FullName

Once I have the type name to list the Enumeration values, I can cast it to a type object and use the static method .GetNames() to list all available labels.

$enumType = [System.Management.Automation.ActionPreference]
[System.Enum]::GetNames($enumType)

SilentlyContinue
Stop
Continue
Inquire
Ignore
Suspend
Break

A PowerShell terminal window demonstrating how to retrieve and list .NET enumeration names and integer values for ActionPreference using the GetNames and Parse methods, displayed as a formatted table with Name and Value columns.

Here is the advanced information to list names and integer values, In general enumeration names are internally represented by integer values by default starting at 0. To see both the name and its corresponding integer value, you can loop through the results 

$enumType = [System.Management.Automation.ActionPreference]
[System.Enum]::GetNames($enumType) | ForEach-Object {
    $name = $_
    $value = [int]([System.Enum]::Parse($enumType, $name))
    [PSCustomObject]@{
        Name = $name
        Value = $value
    }
}

Name             Value
----             -----
SilentlyContinue     0
Stop                 1
Continue             2
Inquire              3
Suspend              4
Ignore               5

This method is useful for any parameter that uses a defined enumeration, such as the -ExecutionPolicy parameter of Set-ExecutionPolicy or the -MemberType of Get-Member. Below is 2 more simpler methods to get information about enumerations with one liner commands.

[Microsoft.PowerShell.Execution].GetEnumNames()

(Get-Command Set-ExecutionPolicy).Parameters['ExecutionPolicy'].ParameterType.GetEnumNames()

Useful Articles
How to Install and Use Microsoft PowerShell on Linux
Configure PowerShell remoting between Windows and Linux
Get-PSRepository WARNING Unable to find module repositories
Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send
Creating an internal PowerShell module repository
How to sign PowerShell ps1 scripts
PowerShell Convert MAC address to Link-local address IPv6
PowerShell fix repair The trust relationship between this workstation and the primary domain failed
Resovled issue with PowerShell - Trust relationship Rejoin computers in domain without restart
PowerShell Invoke-WebRequest The request was aborted Could not create SSL TLS secure channel
PowerShell Invoke-WebRequest The underlying connection was closed: Could not establish trust relationship for the SSL TLS secure channel.

Go Back

Comment

Protected by Mathcha

Blog Search

Page Views

1 4 6 7 6 2 9 7

Archive

Follow me on Blogarama