description | ms.date | ms.topic | title |
---|---|---|---|
Use ShouldProcess For State Changing Functions |
12/05/2024 |
reference |
UseShouldProcessForStateChangingFunctions |
Severity Level: Warning
Functions whose verbs change system state should support ShouldProcess
. To enable the
ShouldProcess
feature, set the SupportsShouldProcess
argument in the CmdletBinding
attribute.
The SupportsShouldProcess
argument adds Confirm and WhatIf parameters to the function. The
Confirm parameter prompts the user before it runs the command on each object in the pipeline.
The WhatIf parameter lists the changes that the command would make, instead of running the
command.
Verbs that should support ShouldProcess
:
New
Set
Remove
Start
Stop
Restart
Reset
Update
Include the SupportsShouldProcess
argument in the CmdletBinding
attribute.
function Set-ServiceObject
{
[CmdletBinding()]
param
(
[string]
$Parameter1
)
...
}
function Set-ServiceObject
{
[CmdletBinding(SupportsShouldProcess = $true)]
param
(
[string]
$Parameter1
)
...
}