Explicitly supply the -WhatIf parameter:
rm foo.txt -WhatIf
Result:
What if: Performing operation "Remove File" on Target "C:\temp\foo.txt".
SupportsShouldProcess attribute automatically propagates -WhatIf to supported cmdlets:
function do-stuff
{
[CmdletBinding(SupportsShouldProcess=$True)]
param([string]$file)
Remove-Item $file
}
do-stuff foo.txt -WhatIf
Result:
What if: Performing operation "Remove File" on Target "C:\temp\foo.txt".
Explicitly use ShouldProcess() method to determine whether -WhatIf was passed:
function do-stuff
{
[CmdletBinding(SupportsShouldProcess=$True)]
param([string]$file)
if ($PSCmdlet.ShouldProcess($file)) {
Write-Host "Deleting file"
Remove-Item $file
}
}
do-stuff foo.txt -WhatIf
Result:
What if: Performing operation "do-stuff" on Target "foo.txt".
SupportsShouldProcess attribute automatically propagates -WhatIf to nested functions:
function do-stuff
{
[CmdletBinding(SupportsShouldProcess=$True)]
param([string]$file)
if ($PSCmdlet.ShouldProcess($file)) {
Write-Host "Deleting file"
Remove-Item $file
}
inner "text"
}
function inner
{
[CmdletBinding(SupportsShouldProcess=$True)]
param([string]$s)
if ($PSCmdlet.ShouldProcess($s)) {
Write-Host "Inner task"
}
$s | out-file "temp9.txt"
}
do-stuff foo.txt -WhatIf
Result:
What if: Performing operation "do-stuff" on Target "foo.txt".
What if: Performing operation "inneYou are probably referring to the -WhatIf and -Confirm parameters on cmdlets. You can read up about them in Get-Help about_commonParameters:
Risk Management Parameter Descriptions
-WhatIf[:{$true | $false}]Displays a message that describes the effect of the command, instead of executing the command.
WhatIf parameter overrides the value of the
$WhatIfPreferencevariable for the current command. Because the default value of the$WhatIfPreferencevariable is0(disabled), WhatIf behavior is not performed without the WhatIf parameter. For more information, type the following command:get-help about_preference_variablesValid values:
$true(-WhatIf:$true). Has the same effect as-WhatIf.
$false(-WhatIf:$false). Suppresses the automatic WhatIf behavior that results when the value of the $WhatIfPreference variable is 1.For example, the following command uses the
WhatIfparameter in aRemove-Itemcommand:PS> remove-item date.csv -whatifInstead of removing the item, Windows PowerShell lists the operations it would perform and the items that would be affected. This command produces the following output:
What if: Performing operation "Remove File" on Target "C:\ps-test\date.csv".-Confirm[:{$true | $false}]Prompts you for confirmation before executing the command.
The Confirm parameter overrides the value of the $ConfirmPreference variable for the cur