Home Ask Login Register

Developers Planet

Your answer is one click away!

TinyRacoon February 2016

Why do I need to use 'parametersetname' with a PowerShell switch?

My question is about the parameter field 'parametersetname' and it mysterious need to be included.

I have this PowerShell (v3) script called test.ps1:


If I run it in the PowerShell IDE cmd line like this:


I get

> False

whereas calling it with

.\test.ps1 -foo

I get

> True

Both good. However it has taken me a while (ages) to realise that for switch to work, I must add this descriptor "parametersetname".

Because if I repeat my test, but omit "parametersetname" like this:


When I run this:


I get error:

Cannot process argument transformation on parameter 'foo'. Cannot convert the "False" value of type "System.Management.Automation.SwitchParameter" to type "System.Management.Automation.ParameterAttribute".

whereas calling it with

.\test.ps1 -foo

I get

Missing an argument for parameter 'foo'. Specify a parameter of type 'System.Management.Automation.ParameterAttribute' and try again.

Does anyone know why?

What magic is 'parametersetname' doing?


Alexis Coles February 2016

You don't need to use parametersetname, but you do need the brackets after Parameter.

If you omit the brackets it seems that powershell is reading it as type information for the parameter and trying to cast the parameter to that type, instead of treating it as a parameter attribute.


Works as does


Post Status

Asked in February 2016
Viewed 2,922 times
Voted 9
Answered 1 times


Leave an answer

Quote of the day: live life