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?
A great answer is provided over at Stackoverflow (all credit to manojlds):
https://stackoverflow.com/questions/8525572/powershell-colon-in-commandlet-parameters
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.