Investigating ConfirmImpact (Part 2: Script Author Perspective)

by Feb 24, 2023

As explained in the previous part, PowerShell uses the $ConfimPreference automatic variable as part of its risk mitigation system: whenever a PowerShell command’s own “ConfirmImpact” is higher or equal to the setting in $ConfirmPreference, PowerShell will show an automatic confirmation dialog.

As a function or script author, you set the “impact level” of your function or script with the attribute CmdletBinding():

function Remove-Something
{
    [CmdletBinding(ConfirmImpact='Medium')]
    param
    (
        [Parameter(Mandatory)]
        [string]
        $Name
    )

    "You entered $Name" 

}

The sample function Remove-Something has declared its ConfirmImpact to be “Medium”, so when you run it, PowerShell will trigger auto-confirmation when $ConfirmPreference is set to at least “Medium”.

Binary cmdlets do the very same. To find out the confirm impact of binary cmdlets, you can use the function below:

filter Get-ConfirmInfo 
{
    param
    (
        [Parameter(Mandatory,ValueFromPipeline)]
        [string]
        $CommandName
    )

    $CommandInfo = Get-Command -Name $CommandName
    [System.Management.Automation.CommandMetaData]::new($CommandInfo) |
    Select-Object -Property Name, ConfirmImpact, SupportsShouldProcess
}

When you submit command names to it, it will reveal the ConfirmImpact defined by the command author, plus whether the command supports simulation switches such as -WhatIf (in which case SupportsShouldProcess shows $true).

 
PS> 'Get-Random', 'Remove-Item', 'Stop-Process' | Get-ConfirmInfo

Name         ConfirmImpact SupportsShouldProcess
----         ------------- ---------------------
Get-Random          Medium                 False
Remove-Item         Medium                  True
Stop-Process        Medium                  True   
 

Since Get-ConfirmInfo is pipeline-aware, you can even investigate your PowerShell command set and search for High impact cmdlets:

 
PS> Get-Command -Verb Remove | Get-ConfirmInfo | Where-Object ConfirmImpact -eq High

Name                        ConfirmImpact SupportsShouldProcess
----                        ------------- ---------------------
Remove-CIAccessControlRule           High                  True
Remove-CIVApp                        High                  True
Remove-CIVAppNetwork                 High                  True
Remove-CIVAppTemplate                High                  True
Remove-BCDataCacheExtension          High                  True
Remove-ClusterAffinityRule           High                  True
Remove-ClusterFaultDomain            High                  True 
(...)  
 

Tweet this Tip! Tweet this Tip!