4

In a script, I need to manage the membership of a group. Unfortunately, the cmdlet for removing a member from a group, Remove-ADGroupMember is always asking for confirmation. This is contradictory to the described behavior of the cmdlet, as there is a -Confirm option that is supposed to turn on confirmation. This requires using obscure and poorly documented colon binding of a value to a switch parameter: -Confirm:$false, when it would make much more sense to instead use a straightforward -Force switch.

Is there a setting in the environment that is changing how the cmdlet behaves? Is this just a poorly implemented feature? Am I missing some obvious documentation that would explain the confusing behavior of switch parameters?

2

Are you asking why not including "-Confirm" prompts you for confirmation? By default, without specifying $false the cmdlet will always prompt for confirmation. The option is there to give you the ability to suppress the confirmation. It may be confusing in that it is called -Confirm but that is because with PS you specify a value for that parameter. So you are in essence saying "set -Confirm to false/no". It would be much more confusing if the parameter were called -NoConfirm and you had to set a value for that parameter!

This is by design. The default is to prompt when you run this cmdlet without the -Confirm:$false to be sure that you wanted to run the command.

Some links for knowledge (note they don't answer the question directly, just give you some insight into PS grammar/syntax):

http://blogs.msdn.com/b/powershell/archive/2006/05/10/594535.aspx

http://www.manning.com/payette/AppCexcerpt.pdf

http://technet.microsoft.com/en-us/magazine/jj554301.aspx

EDIT: maybe I misinterpreted your question. I was basing my answer on "there is a -Confirm option that is supposed to turn on confirmation". If your question is why do I have to use a colon then @DavidV's answer is right on the money.

  • Both answers are relevant. I am asking why the AD cmdlet requires an obscure (passing values to switch arguments is obscure) syntax to work normally. Was it deliberate? Is there a reason they chose not to have a -NoConfirm? I am just annoyed and frustrated wasting a few hours hunting down an answer to a problem that shouldn't have been an issue in the beginning. Why bother with a -Switch parameter that cannot even be used as a straight switch parameter? – Myrddin Emrys Jun 5 '13 at 16:27
  • The "why" can really only be addressed by the PS Team at MS. But (IMO) the reasoning is that you typically don't have a parameter that defaults to a negative result in PS. So having a -NoConfirm switch would still need a value (-NoConfirm:$True) which would be odd syntax here since the default/understood command here asks for confirmation. So with proper PS syntax by stating "-Confirm:$False" you are saying "change the Confirm parameter to false in this instance". I've modded my answer to include a few links to check out. – TheCleaner Jun 5 '13 at 18:16
  • 1
    @MyrddinEmrys I think you mean why didn't they include a -Force switch to suppress confirmation, as this -Confirm and it's behavior aren't unique to the AD Module. – MDMoore313 Jun 6 '13 at 16:51
  • Thank you MDMoore, that is an excellent re-phrasing of my question, and quite accurate. – Myrddin Emrys Jun 6 '13 at 20:47
  • I should also note @TheCleaner that a -Switch that does nothing (when present it has the same behavior as when absent) is far more confusing than a switch with a negative name such as -NoConfirm. – Myrddin Emrys Jun 6 '13 at 20:52
1

A great answer is provided over at Stackoverflow (all credit to manojlds):

https://stackoverflow.com/questions/8525572/powershell-colon-in-commandlet-parameters

  • This does explain how to pass a value to a switch parameter. It does not explain the reasoning for a command to include a switch parameter that does nothing unless you use an unusual syntax that is never used except in this odd case of a bad default setting. – Myrddin Emrys Jun 5 '13 at 16:28
  • 'It is in scenarios where the value for a switch variable itself has to be determined from another variable that the : is used.' So when using -Confirm $false, $false as a value is not being passed to the switch it's just seen as a separate argument. Whereas if you use -Confirm:$false you are telling the switch to use the value of '$false'. – David V Jun 5 '13 at 16:56
  • I'm aware of how to pass a parameter to a switch... but the question is why are they using a switch parameter in the first place? Switch parameters are meant to be absent (off) or present (on). That is their function, and because of that the :$parameter syntax is (rightfully) rarely used and poorly documented. – Myrddin Emrys Jun 6 '13 at 21:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.