February 2019
Intermediate to advanced
626 pages
15h 51m
English
ShouldProcess is used to support WhatIf and is responsible for showing confirmation preferences based on the impact level of a command.
ShouldProcess is also used to prompt for confirmation when a command is performing a higher-risk action.
The following example will display a message instead of running the Write-Host statement when the WhatIf parameter is supplied:
function Test-ShouldProcess { [CmdletBinding(SupportsShouldProcess)] param ( ) if ($pscmdlet.ShouldProcess('SomeObject')) { Write-Host 'Deleting SomeObject' -ForegroundColor Cyan }}
When run using the WhatIf parameter, the command will show the following message:
PS> Test-ShouldProcess -WhatIfWhat if: Performing the operation "Test-ShouldProcess" on target "SomeObject". ...
Read now
Unlock full access