Skip to content

Latest commit

 

History

History
78 lines (63 loc) · 1.85 KB

UseShouldProcessForStateChangingFunctions.md

File metadata and controls

78 lines (63 loc) · 1.85 KB
description ms.date ms.topic title
Use ShouldProcess For State Changing Functions
12/05/2024
reference
UseShouldProcessForStateChangingFunctions

UseShouldProcessForStateChangingFunctions

Severity Level: Warning

Description

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

How

Include the SupportsShouldProcess argument in the CmdletBinding attribute.

Example

Wrong

function Set-ServiceObject
{
    [CmdletBinding()]
    param
    (
        [string]
        $Parameter1
    )
    ...
}

Correct

function Set-ServiceObject
{
    [CmdletBinding(SupportsShouldProcess = $true)]
    param
    (
        [string]
        $Parameter1
    )
    ...
}

More information