How To Write Debug Messages To The Console In PowerShell

In this video, Kenward will cover using the Write-Debug cmdlet, the $DebugPreference automatic variable, using debugging in PowerShell advanced functions and will explain how to debug stream works.

Hi I'm going to show you how to write debug messages to the console in powershell. So the way we write debug messages to the console simply is to use the write debug commandlet. However, by default, the write debug commandlet won’t output anything to the screen. So if we execute write debug. We can see it doesn't output anything to the screen by default. However, if we set the debug preference to continue. Write debug will output to the screen. And I'm gonna set the debug preference back to the default and show you a function. So you can set your functions to write debug messages and to do that. You simply have to make your function in advanced function by adding commandlet binding and Param to that function so to show you what I mean, I'm going to load this demo function into memory just going to execute it to show you what it looks like normally and all it does is output hello world. And because that function doesn't have the dash debug switch, it's not kind of output the debug message. However, with this debug switch it does output the debug message Ann asks us if we want to continue. So one last thing to show you about debug debug is a separate stream from the standard output stream and I can show you what I mean by this by assigning the output of this function to a variable. Like this. And you can see we do get the debug message here because we have the debug switch here and will say yes to continue and when I look at the output of dollar. A It says hello world, it's getting that from the write output. But it is not including the write debug because write debug is on the debug stream. We are only capturing the standard output in Dollar, A and so that's how to Debug messages to the console in powershell.