Cmdlet.ShouldProcess Method (String, String)


Updated: April 27, 2016

Applies To: Windows PowerShell

Requests confirmation from the user before an operation is performed. This method sends the operation that is to be performed and the name of the resource to be changed so that the user can decide if the operation should continue.

Namespace:   System.Management.Automation
Assembly:  System.Management.Automation (in System.Management.Automation.dll)

public bool ShouldProcess(
	string target,
	string action


Type: System.String

The name of the resource to be changed. To see how the resource is displayed in the confirmation message, see the following code examples.

Type: System.String

The operation to be performed. To see how the operation is displayed in the confirmation message, see the following code examples.

Return Value

Type: System.Boolean

A Boolean object that indicates true if the cmdlet should perform the action. A returned value of false indicates that the action should not be performed.

Exception Condition

The pipeline is stopped. The pipeline was terminated either before the call was made or while the call was in progress. By default, the cmdlet should allow the caller of the processing record method to catch this exception.

Also, if the pipeline was terminated because of ActionPreference.Stop or ActionPreference.Inquire, the command failure will ultimately be ActionPreferenceStopException.


The call cannot be completed at this time or cannot be completed from this thread. For more information, see the following Remarks section.

This method should be called before the cmdlet makes a change to the system. For more information about confirmation requests, including information about how confirmation requests are affected by the $ConfirmPreference shell variable, see Requesting Confirmation.

To call this method, the cmdlet must also set the SupportsShouldProcess parameter of its cmdlet attribute declaration to true. For more information about the syntax and declaration of this attribute, see CmdletAttribute Declaration.

This method can be called only from within the cmdlet input processing methods (BeginProcessing, ProcessRecord, and EndProcessing) and only from that thread. If this call is made from outside these methods or from another thread, an InvalidOperationException exception is thrown.

When this method is called, the runspace checks any command-line settings or preference variables when it determines what should be returned or what is displayed to the user.

For more information about cmdlets, see Windows PowerShell Cmdlets.

In the following code example, the ShouldProcess call specifies the resource that the cmdlet will act upon and the operation to be performed.

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

This is the message that is returned when the Confirm parameter is specified.

Are you sure you want to perform this action?
Performing operation "ShouldProcess action" on Target "ShouldProcess target".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"):

This is the second message that is returned when the Confirm parameter is specified and the user answers Yes or Yes to All to the first message. In these cases, the call to the ShouldContinue method is made.

Continue with this operation?
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"):
Return to top

Community Additions