0

ShouldContinue vs ShouldProcess: What’s The Difference?

ShouldContinue vs ShouldProcess: What’s the difference? If you’re writing advanced functions and using the SupportsShouldProcess CmdletBinding parameter you should be aware of the difference between these 2 options. While they may seem subtle on the outside, they may have some impact on your automation if not used properly.

One thing I should note as well is that this addition now allows your commands to utilize the -Confirm and -Whatif parameters that can be used for testing.

ShouldContinue vs ShouldProcess

Here are few highlights for each item that are worth mentioning.

ShouldContinue

  • Used to prompt the user by default
  • Needs a Force parameter added to avoid syntax errors in VSCode
  • Requires at least 2 messages for the prompts

ShouldProcess

  • Will NOT prompt user by default
  • Confirm parameter must be used to prompt for action
  • Requires at least 1 message for the prompts

Example Code Snippet

function Test-SupportsShouldProcess {
    [CmdletBinding(
        SupportsShouldProcess,
        ConfirmImpact='Medium'
    )]
    param(
        [Parameter(
            Mandatory = $false
        )]
        [switch]    $ShouldContinueParam,

        [switch]    $ShouldProcessParam,

        [switch]    $Force

    )

    if ($ShouldContinueParam) {
        if ($Force -or $PSCmdlet.ShouldContinue("Would you like to continue", "Should Process Testing")) {
            Write-Output ''
            Write-Output 'Should continue has been called with the force parameter or typed "Y" when prompted'
        }
    }

    if ($ShouldProcessParam) {
        if ($PSCmdlet.ShouldProcess("Should Process Testing") ) {
            Write-Output ''
            Write-Output 'Should Process has been called and should only be prompted with the confirm parameter'
        }
    }
}


 

SupportsShouldProcess ShouldContinue

 

SupportsShouldProcess ShouldProcess

Conclusion

I hope this has cleared up some questions regarding ShouldContinue vs ShouldProcess and how to use each. If you’re up for it, feel free to check out our other Powershell Scripts since there are plenty there to use out in the real world.

Also, @theSysadminChannel on Youtube can be useful if you’re looking for free sysadmin video content on the internets.

Rate this post

Paul

Hi, my name is Paul and I am a Sysadmin who enjoys working on various technologies from Microsoft, VMWare, Cisco and many others. Join me as I document my trials and tribulations of the daily grind of System Administration.