Parameter issue

I have a script with many Parameters, one of them being -myParameterExample … here is a summary (items left out for simplicity):

[CmdletBinding()]

param (
	........, 
	........, 
	........, 
	[Switch]$myParameterExample = $False,
	........, 
	........
)

Yet, when run the script and enter:

.\MyScript -myParameter

The parameter -myParameterExample returns true. Am I missing something, how come it picks up the partial entry? Googled but did not find much, not really sure what to google for either. Thanks in advance. I am sure it is something stupid on my part.

Switch parameters don’t take a value (and you shouldn’t set a default value), they default to false.

A switch parameter returns true when it’s present, unless you explicitly set it to false.

.\MyScript -myParameter:$false

OK, and yes, I already read all that, but the problem is that it should only match with the parameter of:

-myParameterExample

yet is it matching on

-myParameter

Maks sense? I am under the impression it should only match on the entire string, not part of it. And I tried without setting to $False, same thing.

You can abbreviate ALL parameter names if you like. As long as they are unique it will work. And if one character is enough to specify the parameter name it’s enough. That’s a default feature of all parameternames in PowerShell. But you could not use only -F when the cmdlet has parameters -File and -Force for example. In this case you’d need either -Fi or -Fo.

About the issue of a default value … If you want to have a default value you may use a type of [Boolean] instead.

2 Likes

Good to know Olaf, many thanks. I had no idea you could abbreviate. Is there any docs at M$ on this and or how to disable this?

As for the default, I am not actually setting that in the script, I was playing around and somehow left that in the post.

Thanks !!

I don’t know if there is an explicit documentaiton for this and as far as I know you cannot disable this behaviour. Why would you want to, actually? :man_shrugging:t4: :wink:

I was just curious if it could be disabled … don’t really want to, just trying to learn. Thanks again :slight_smile:

You may look at StrictMode. I know it tightens a lot.

Thanks KrzyDoug, I will have a look.