No, wait – I mean to be confirmed or not to be confirmed. Or is it “I am confirming” or “I am not confirming?” Oh hell…
Yep, here is another PowerShell idiom on which I stub my metaphorical cognitive toe every time: the –confirm parameter. Let’s see if this new habit of writing about it changes my brain.
The –confirm parameter is one of those ubiquitous common parameters that shows up in most cmdlets. Even the ones you make yourself. Check it:
function test-confirm {
[cmdletbinding(SupportsShouldProcess=$true)]
param()
process {
if( $pscmdlet.shouldProcess( "test","test") )
{
write-host "processed"
}
}
}
> test-confirm -?
test-confirm [-Verbose] [-Debug] [-ErrorAction <ActionPreference>] [-WarningAction <ActionPrefe
rence>] [-ErrorVariable <String>] [-WarningVariable <String>] [-OutVariable <String>] [-OutBuff
er <Int32>] [-WhatIf] [-Confirm]
That SupportsShouldProcess parameter of the CmdletBinding attribute is what makes the –confirm parameters show up (sidebar: it also enables the –whatif parameter). It’s a simple switch parameter, making its use trivial:
test-confirm -confirm
The question is: what am I actually saying when I use the –confirm parameter? Here are two choices:
- I am confirming this operation, so do not prompt me for confirmation.
- I wish to be asked to confirm this operation, so please prompt me for it.
I always always think it’s 1, when it’s actually choice 2. It’s a strange semantic, and in truth I can think of few situations where you want to force a confirmation prompt. Generally the –confirm parameter is used to suppress these prompts by explicitly setting the switch to false:
test-confirm -confirm:$false
This feels awkward and in my opinion reads poorly – something about saying “no” to a parameter named –confirm and then having the command execute silently feels wrong. I think it’s because I think of “confirming” as something I’m supposed to do, when it’s actually something PowerShell does. In other words, the act of confirming is PowerShell asking me whether to proceed, and not my answer to that question.
Learning More
The PowerShell help contains a ton of info on –confirm and the other common parameters. Check out these help topics to learn more:
- about_CommonParameters
- about_Functions_CmdletBindingAttribute