Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Use on existing cmdlets</h2> <p>Explicitly supply the -WhatIf parameter:</p> <pre><code>rm foo.txt -WhatIf </code></pre> <p>Result:</p> <pre><code>What if: Performing operation "Remove File" on Target "C:\temp\foo.txt". </code></pre> <h2>Use on existing cmdlets within a function or script</h2> <p><code>SupportsShouldProcess</code> attribute automatically propagates <code>-WhatIf</code> to supported cmdlets:</p> <pre><code>function do-stuff { [CmdletBinding(SupportsShouldProcess=$True)] param([string]$file) Remove-Item $file } do-stuff foo.txt -WhatIf </code></pre> <p>Result:</p> <pre><code>What if: Performing operation "Remove File" on Target "C:\temp\foo.txt". </code></pre> <h2>Use on your own code blocks</h2> <p>Explicitly use <code>ShouldProcess()</code> method to determine whether <code>-WhatIf</code> was passed:</p> <pre><code>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 </code></pre> <p>Result:</p> <pre><code>What if: Performing operation "do-stuff" on Target "foo.txt". </code></pre> <h2>Use on Nested functions in the <em>same</em> module</h2> <p><code>SupportsShouldProcess</code> attribute automatically propagates <code>-WhatIf</code> to nested functions:</p> <pre><code>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 </code></pre> <p>Result:</p> <pre><code>What if: Performing operation "do-stuff" on Target "foo.txt". What if: Performing operation "inner" on Target "text". What if: Performing operation "Output to File" on Target "temp9.txt". </code></pre> <h2>Use on Nested functions in a <em>different</em> module</h2> <p>Unfortunately, -WhatIf does not propagate automatically to functions defined in a different module. See <a href="https://stackoverflow.com/questions/7984876/powershell-how-to-get-whatif-to-propagate-to-cmdlets-in-another-module">Powershell: How to get -whatif to propagate to cmdlets in another module</a> for discussion and workaround for this.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload