PowerShell can't use the matching enum type? PowerShell can't use the matching enum type? powershell powershell

PowerShell can't use the matching enum type?


You are running into a small gotcha regarding parsing modes. You can put parens around the argument and it will work:

test ([System.ServiceProcess.ServiceControllerStatus]::Stopped)

Alternatively, conversions from string to enum happen naturally, so you could write:

test Stopped

Here are a couple good links that discuss parsing modes:


You can pass an enum value as a string but you don't pass the typename as part of the argument e.g. this works just fine:

PS> test StoppedStopped System.ServiceProcess.ServiceControllerStatus

That said, when I'm calling .NET methods I prefer to use the fully qualified enum value instead of a string. That's because .NET methods tend to have multiple overloads and those that take strings can confuse PowerShell when to comes to picking the right overload.


Apparently, PowerShell thinks I am sending it a string, rather than an enum object. You get the same error message if you quote the fully qualified name:

PS> test '[System.ServiceProcess.ServiceControllerStatus]::Stopped'test : Cannot process argument transformation on parameter 'x'. Cannot convert value"[System.ServiceProcess.ServiceControllerStatus]::Stopped" to type "System.ServiceProcess.ServiceControllerStatus".Error: "Unable to match the identifier name [System.ServiceProcess.ServiceControllerStatus]::Stopped to a validenumerator name.  Specify one of the following enumerator names and try again: Stopped, StartPending, StopPending,Running, ContinuePending, PausePending, Paused"At line:1 char:6+ test '[System.ServiceProcess.ServiceControllerStatus]::Stopped'+      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    + CategoryInfo          : InvalidData: (:) [test], ParameterBindingArgumentTransformationException    + FullyQualifiedErrorId : ParameterArgumentTransformationError,test

Putting the enum in parentheses to force PowerShell to evaluate it first does the trick:

PS> test ([System.ServiceProcess.ServiceControllerStatus]::Stopped)Stopped System.ServiceProcess.ServiceControllerStatus