How To Write Verbose Messages To The Console In PowerShell

Life isn’t just about the PowerShell Write-Host cmdlet even though lots of PowerShell newbies think so! There are so many other streams in the ocean. Wait. That doesn’t sound right. Anyway, Kenward takes us on a journey in this video to show us the Verbose stream and how we can use the Write-Verbose cmdlet. Kenward, he’s so clever.

I'm going to show you how to write verbose messages to the console in powershell. The way we do that is with the commandlet write verBose. However, by default. The verbose is setting verbose dollar verbose preference is set to silently continue and with that it will not write the verbose message out to the screen to show you what I mean, so if I execute that write verbose command with the default verbose setting. It's not going to display anything to the screen. However, if I change the dollar verbose preference to continue. Then when we execute the write verbose commandlet it does output to the screen. So that's a quick way that you can use write verbose to write verbose messages. However, you can also use this in your own functions. So you just have to make your function advanced function by adding commandlet binding. parameter and that will give you the common parameters and one of those common parameters is verbose. So I can show you what I mean, I'll execute this code and put this. Function in the memory and when we execute this function. It does its regular output, but it will not write verbose. If I use the common parameter dash verbose. Then it will output the verbose commands one important thing to know is that the verbose output is a separate stream from your regular output. You can use this to your advantage. If you want to display messages to the user. But you want to also collect the output of that command the regular output. So this function here. Outputs hello world to the regular output stream and then to the verbose output stream it outputs hello friends. So if I execute this and put the output to a variable here for example, Dollar A. We still see the verbose output and the contents of dollar a we can see. Are hello world, so an interesting use of verbose? That's our quick intro, to verbose.