I’ve written a few scripts and programs lately, mostly just for fun. In those scripts, I’ve used Write-Host to return output. To me, it’s been like “Print” in various languages where I can get output of a program. Often I’ll use a method/function to get info and then use print to output that to the caller.
However a few people noted that in my last script, Write-host wasn’t necessary. When I asked why, both Mike Fal and Drew Furgiule responded. I got these two items:
I learned something new today. I had assumed that I’d need a way to get the output to the screen and manage output with my own logic. I’ve had this before
$debug = 1
if $debug = 1 { write-host $somevariable}
However a quick check shows this isn’t really what I want. Instead, I’d use Write-Debug or Write-Verbose. A quick test shows I can do this:
That’s much better than passing in a debug parameter or changing a value as I run scripts.
Looks like I have some refactoring to do.


Thanks for the information you shared.
I am using PowerShell scarcely even in the version SQL Server but it is possible that your post will incite me to learn it in a more deep way.
Your two links seem interesting but I will need to reread them as I am not sure to have understood them in the full way. Sorry , I stopped to learn the English language more forty years ago and I am using it only to post on forums , which is explaining my difficulties to get a good understanding of it.
I hope you will go on to post articles as interesting as this one.
LikeLike
I’ll keep posting. Good luck learning. PoSh is tricky at times, but it helps to practice.
LikeLike