title: Powershell: How to get -whatif to propagate to cmdlets in another module
body: <p>I've been trying write safe code that supports -whatif with the ShouldProcess method so my users have an idea of what a cmdlet is supposed to do before they run it for real.</p>
  
  <p>However I’ve run into a bit of a snag. If I call a script with -whatif as an argument, $pscmdlet.ShouldProcess will return false. All well and good. If I call a cmdlet defined in the same file (that has SupportsShouldProcess=$true) it will return false as well. </p>
  
  <p>However, if I am calling a cmdlet defined in another module I have loaded using Import-Module, it will return true. The -whatif context does not seem to get passed through to calls in the other module.</p>
  
  <p>I don't want to have to manually pass in a flag to every cmdlet. Does anyone have a better solution?</p>
  
  <p>This issue seems related to this <a href="https://stackoverflow.com/questions/2278876/powershell-scripting-recommended-way-to-implement-shouldprocess-when-function-c">question</a>. However, they are not talking about the cross-module problem.</p>
  
  <p>Example Script:</p>
  
  <pre><code>#whatiftest.ps1
  [CmdletBinding(SupportsShouldProcess=$true)]
  param()
  
  Import-Module  -name .\whatiftest_module  -Force
  
  function Outer
  {
      [CmdletBinding(SupportsShouldProcess=$true)]
      param()
      if( $pscmdlet.ShouldProcess("Outer"))
      {
          Write-Host "Outer ShouldProcess"
      }
      else
      {
          Write-Host "Outer Should not Process"
      }
  
      Write-Host "Calling Inner"
      Inner
      Write-Host "Calling InnerModule"
      InnerModule
  }
  
  function Inner
  {
      [CmdletBinding(SupportsShouldProcess=$true)]
      param()
  
      if( $pscmdlet.ShouldProcess("Inner"))
      {
          Write-Host "Inner ShouldProcess"
      }
      else
      {
          Write-Host "Inner Should not Process"
      }
  }
  
      Write-Host "--Normal--"
      Outer
  
      Write-Host "--WhatIf--"
      Outer -WhatIf
  </code></pre>
  
  <p>The Module:</p>
  
  <pre><code>#whatiftest_module.psm1
   function InnerModule
   {
      [CmdletBinding(SupportsShouldProcess=$true)]
      param()    
  
      if( $pscmdlet.ShouldProcess("InnerModule"))
      {
          Write-Host "InnerModule ShouldProcess"
      }
      else
      {
          Write-Host "InnerModule Should not Process"
      }
   }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>F:\temp&gt; .\whatiftest.ps1
  --Normal--
  Outer ShouldProcess
  Calling Inner
  Inner ShouldProcess
  Calling InnerModule
  InnerModule ShouldProcess
  --WhatIf--
  What if: Performing operation "Outer" on Target "Outer".
  Outer Should not Process
  Calling Inner
  What if: Performing operation "Inner" on Target "Inner".
  Inner Should not Process
  Calling InnerModule
  InnerModule ShouldProcess
  </code></pre>