title: Why is $DebugPreference getting lost in a module?
body: <p>I have a powershell script and I have set the <code>$DebugPreference</code> to <code>"Continue"</code>. However when I call <code>Write-Debug</code> from a module that is called from my script, the <code>$DebugPreference</code> changed to <code>"SilentlyContinue"</code>. Why is that? How can I keep <code>$DebugPreference</code> the same as the calling script? Example below</p>
  
  <p><strong>CallingScript.ps1</strong></p>
  
  <pre><code>$DebugPreference = "Continue"
  Write-Host "Debug preference: $DebugPreference"
  Write-Debug "Checking that debugging works"
  Import-Module Logging;
  Write-Log "Debug" "Checking that debugging still works!"
  </code></pre>
  
  <p><strong>Logging.psm1</strong></p>
  
  <pre><code>Function Write-Log
  {
      param (
      [ValidateSet("Error","Warning","Debug","Info")][String]$type,
      [String]$logMessage
      )
      Write-Host "Debug preference: $DebugPreference"
      switch($type)
      {
          "Error" {Write-Error $logMessage;}
          "Warning" {Write-Warning $logMessage;}
          "Debug" {Write-Debug $logMessage;}
          "Info" {Write-Output $logMessage;}
  
      }
  }
  </code></pre>
  
  <p>If I run the script, this is the output:</p>
  
  <pre><code>PS &gt; .\CallingScript.ps1
  Debug preference: Continue
  DEBUG: Checking that debugging works
  Debug preference: SilentlyContinue
  PS &gt;
  </code></pre>