Blog Post

PowerShell–Don’t Use Write-Host

,

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:

2016-03-02 12_10_16-Windows PowerShell ISE

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.

Filed under: Blog Tagged: powershell, syndicated

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating