Enum like switch parameter in PowerShell

Powershell

Powershell Problem Overview


I am using switch parameters in my PowerShell script in this fashion.

param(
	[switch] $Word,
	[switch] $Excel,
	[switch] $powerpoint,
	[switch] $v2007,
	[switch] $v2010,
	[switch] $x86,
	[switch] $x64,
)

I am trying to figure out any neat way to have it more enum style. As anyone might guess, I would want the user to choose between word, excel and powerpoint. And between x2007 and v2010.

Is there a neat way to get input params enum style?

I am new to PowerShell. So if this sounds like that I don't know something obvious, then please point me to some link where I can read about it.

Powershell Solutions


Solution 1 - Powershell

I would use a ValidateSet parameter attribute instead.

From: about_Functions_Advanced_Parameters

> The ValidateSet attribute specifies a set of valid values for a > parameter or variable. Windows PowerShell generates an error if a > parameter or variable value does not match a value in the set.

Example function:

function test-value
{
	param(
		[Parameter(Position=0)]
		[ValidateSet('word','excel','powerpoint')]
		[System.String]$Application,

		[Parameter(Position=1)]
		[ValidateSet('v2007','v2010')]
		[System.String]$Version
	)
	

	write-host "Application: $Application"
	write-host "Version: $Version"
}	


PS > test-value -application foo

Output:

test-value : Cannot validate argument on parameter 'Application'. The argument "foo" does not belong to the set "word,excel,powerpoint" specified by the ValidateSet attribute. Supply an argument that is in the set and then try the command again.

Solution 2 - Powershell

You can use the ValidateSet attribute:

function My-Func
{
    param (
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [ValidateSet('Word', 'Excel', 'PowerPoint', 'v2007', 'v2010', 'x86', 'x64')]
        [String]$MyParam
    )
    
    Write-Host "Performing action for $MyParam"
}

My-Func -MyParam 'Word'
My-Func -MyParam 'v2007'
My-Func -MyParam 'SomeVal'

Output:

Performing action for Word
Performing action for v2007
My-Func : Cannot validate argument on parameter 'MyParam'. The argument "SomeVal" does not belong to the set "Word,Excel,PowerPoint,v2007,v2010,x86,x64" specified by the ValidateSet attribute. Supply an argument that is in the
 set and then try the command again.
At C:\Users\George\Documents\PowerShell V2\ValidateSetTest.ps1:15 char:17
+ My-Func -MyParam <<<<  'SomeVal'
    + CategoryInfo          : InvalidData: (:) [My-Func], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,My-Func

Solution 3 - Powershell

This blog post by the PowerShell team defines how to do this in PowerShell 1.0. In PowerShell 2.0 you can use Add-Type like so:

C:\PS> Add-Type -TypeDefinition @'
>> public enum MyEnum {
>> A,
>> B,
>> C,
>> D
>> }
>> '@
>>

Update: Here's how to use the enum:

C:\PS> function foo([MyEnum]$enum) { $enum }
C:\PS> foo ([MyEnum]::A)
A

You need the parentheses around the argument to parse the argument as a Type. This is required because arguments are treated more or less like strings. Knowing this, you can also pass them enum in a simple string form and powershell will figure it out:

C:\PS> foo A
A
C:\PS> $arg = "B"
C:\PS> foo $arg
B
C:\PS> foo F
error*

error - F is not one of the enumerated values - valid values include A,B,C,D *

Solution 4 - Powershell

Since PowerShell 5 you can actually use/create an Enum natively.

enum OS {
    Windows
    Linux
    iOS
}

This is then also visible as its type.

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     OS                                       System.Enum

Maybe a useful link by 4sysops.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbitsView Question on Stackoverflow
Solution 1 - PowershellShay LevyView Answer on Stackoverflow
Solution 2 - PowershellGeorge HowarthView Answer on Stackoverflow
Solution 3 - PowershellKeith HillView Answer on Stackoverflow
Solution 4 - PowershellAlex_PView Answer on Stackoverflow