Cmdlet.ShouldProcess Method (String)
Updated: April 27, 2016
Applies To: Windows PowerShell
Requests confirmation from the user before an operation is performed. This method sends the name of the resource to be changed so that the user can decide if the operation should continue.
Assembly: System.Management.Automation (in System.Management.Automation.dll)
The name of the resource to be changed. To see how the resource is displayed in the confirmation message, see the following code examples.
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 themethod, the command making the call must also indicate that it supports these calls.
For cmdlets, set the SupportsShouldProcess parameter of its Cmdlet attribute to true. For more information about the syntax and declaration parameter of this attribute, see CmdletAttribute Declaration.
For functions, set the SupportsShouldProcess parameter of its CmdletBinding attribute to true.
For providers, set the SupportsShouldProcess parameter of its CmdletProvider attribute to true.
This method can be called only from within the 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 Writing a Windows PowerShell Cmdlet.
In the following code example, thecall specifies the resource that the command will act upon. In the confirmation message that is displayed to the user, Windows PowerShell adds the name of the command and the call specifies the resource.