How to Request Confirmations

This example shows how to call the System.Management.Automation.Cmdlet.ShouldProcess and System.Management.Automation.Cmdlet.ShouldContinue methods to request confirmations from the user before an action is taken.

Important

For more information about how Windows PowerShell handles these requests, see Requesting Confirmation.

To request confirmation

  1. Ensure that the SupportsShouldProcess parameter of the Cmdlet attribute is set to true. (For functions this is a parameter of the CmdletBinding attribute.)

    [Cmdlet(VerbsDiagnostic.Test, "RequestConfirmationTemplate1",
            SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.High)]
    

    Note

    Using SupportsShouldProcess alone does not guarantee that the user is prompted for confirmation. Prompting is determined by the value of $ConfirmPreference and the impact of your action. Use ConfirmImpact to set the severity of the impact of your operation.

  2. Add a Force parameter to your cmdlet so that the user can override a confirmation request.

    [Parameter()]
    public SwitchParameter Force
    {
      get { return force; }
      set { force = value; }
    }
    private bool force;
    
  3. Add an if statement that uses the return value of the System.Management.Automation.Cmdlet.ShouldProcess method to determine if the System.Management.Automation.Cmdlet.ShouldContinue method is called.

  4. Add a second if statement that uses the return value of the System.Management.Automation.Cmdlet.ShouldContinue method and the value of the Force parameter to determine whether the operation should be performed.

Example

In the following code example, the System.Management.Automation.Cmdlet.ShouldProcess and System.Management.Automation.Cmdlet.ShouldContinue methods are called from within the override of the System.Management.Automation.Cmdlet.ProcessRecord method. However, you can also call these methods from the other input processing methods.

protected override void ProcessRecord()
{
  if (ShouldProcess("ShouldProcess target"))
  {
    if (Force || ShouldContinue("", ""))
    {
      // Add code that performs the operation.
    }
  }
}

See Also

Writing a Windows PowerShell Cmdlet