ConfirmPreference

ConfirmPreference

Rate This
  • Comments 5

With Windows PowerShell you can specify -CONFIRM on commands that have a side-effect on the system and it will ask you whether to perfmon the action or not.  e.g.

PS> Stop-Process 7004 -Confirm

Confirm
Are you sure you want to perform this action?
Performing operation "Stop-Process" on Target "calc (7004)".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): y
PS>

But you can also configure the system to automatically confirm operations based upon their impact on the system.  When developers write their cmdlets, they can declare the impact of their commands on the system: "HIGH", "MEDIUM", "LOW".  For instance, the Exchange command for Removing a Mailbox is declared to have a HIGH impact.  Stop-Process is defined as "MEDIUM".  You can then set the variable: $ConfirmPreference to be the level that you want Automatic confirmation turned on.  In the example below, we try to stop CALC with $ConfirmPreference set at LOW, MEDIUM, and HIGH.  Notice that autoconfirmation kicks in when it is set to LOW or MEDIUM but not when it is set to HIGH.

PS> Calc
PS> $ConfirmPreference="low"
PS> Stop-Process -ProcessName calc

Confirm
Are you sure you want to perform this action?
Performing operation "Stop-Process" on Target "calc (5852)".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): n
PS> $ConfirmPreference="Medium"
PS> Stop-Process -ProcessName calc

Confirm
Are you sure you want to perform this action?
Performing operation "Stop-Process" on Target "calc (5852)".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): n
PS> $ConfirmPreference="High"
PS> Stop-Process -ProcessName calc
PS>

The default value of $ConfirmPreference is "High".

Enjoy

Jeffrey Snover [MSFT]
Windows PowerShell/MMC Architect

 

When confirmation is turned on by $ConfirmPreference, you can turn it off for any individual cmdlet invocation using "-Confirm:$false".  You can also use "-Confirm:$false" to turn off default confirmation for high impact cmdlets such as Removing a Mailbox.  Another way to turn off confirmation is by setting $ConfirmPreference to "None"; you can limit the effect by setting $script:ConfirmPreference etc, see "get-help about_scope" for more details.

PS> $ConfirmPreference="Medium"
PS> stop-process -ProcessName calc -Confirm:$false
PS> $ConfirmPreference = "None"
PS> stop-process -ProcessName calc
PS>

Confirmation may cause your script to fail if it is running in a non-interactive environment, so it is important to know how to turn confirmation off when necessary.

Jon Newman [MSFT]
Windows PowerShell team

 


Visit the Windows PowerShell Team blog at:    http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:  http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

Leave a Comment
  • Please add 5 and 4 and type the answer here:
  • Post
  • PingBack from http://xaegr.wordpress.com/2006/12/16/shouldprocess/

  • I just noticed this did not make it into the about_automatic_variables help file like Debug, Warning and so on.

  • hi,

    is there any way to force to use default value for confirm. Because i do have comments in ps1 file and it is keep on asking confirm for each step.

    i have tried -Confirm "Y" but getting error

    Thanks

    Appaji

  • You can use -Cofirm:$true or -Confirm:$false to supply a fixed value to confirm.

    Hope this helps,

    James Brundage [MSFT]

  • This does not work with Remove-WebVirtualDirectory and neither does -confirm:$false

Page 1 of 1 (5 items)