title: Print debug messages to console from a PowerShell function that returns
body: <p>Is there a way to print debug messages to the console from a PowerShell function that returns a value?</p>
  
  <p>Example:</p>
  
  <pre><code>function A
  {
      $output = 0
  
      # Start of awesome algorithm
      WriteDebug # Magic function that prints debug messages to the console
      #...
      # End of awesome algorithm
  
      return $output
  }
  
  # Script body
  $result = A
  Write-Output "Result=" $result
  </code></pre>
  
  <p>Is there a PowerShell function that fits this description?</p>
  
  <p>I'm aware of <a href="https://docs.microsoft.com/en-gb/powershell/module/Microsoft.PowerShell.Utility/Write-Output?view=powershell-5.1" rel="nofollow noreferrer">Write-Output</a> and Write-*, but in all my tests using any of those functions inside a function like the one above will not write any debug messages. I'm also aware that just calling the function without using the returned value will indeed cause the function to write debug messages.</p>