Write-Host vs Write-Output

This topic contains 4 replies, has 4 voices, and was last updated by  Vern Anderson 3 years, 5 months ago.

  • Author
    Posts
  • #15277

    Eric Salamone
    Participant

    Mr. Jones from what I have watched or read states that he likes the write-output over the write-host and I understand the reason (object vs text to screen), but what if you wanted some of the benefits from both. I have a ping script that imports the computer names, but I like having the computers that are up a different color compared to the computers that are down. So the script outputting an object vs text is not a big deal, but lets say you did want it to be an object but with different colors for example. Would making the Write-Host into a variable then calling it from the Write-Output a way around that issue or is it kind of pointless?

  • #15278

    Vern Anderson
    Participant

    He just talked about this yesterday in one of his TechEd presentations. He does agree that there are times where Write-Host is the command to use if you need color coded fonts or something like that.

    However the emphasis is there so that you learn that Write-Host once it takes your output destroys the objects and you literally just have pixels on the screen.

    There are many debates on this and I don't want your thread to turn into one, but my take on it might be this. If you're writing scripts to have humans reading output on the screen then use Write-Host if you wish. But true automation doesn't require user input or anyone to even be there when the script runs. So consider the options and determine what is best for your script and use it if you want to.

    -VERN

  • #15279

    JohnRock Bilodeau
    Participant

    Why not simply use write-warning or write-error if the ping fails?

  • #15280

    Don Jones
    Keymaster

    I think it's about the principles of tool design. If that was my goal, I'd write one function named Test-ICMP. That would do the ping, and output objects to the pipeline by using Write-Output. I'd write a second command, Format-ICMPResults, that accepted those objects on the pipeline and formatted them for screen display. That way, I could use Test-ICMP in other places, but when I wanted colored formatting I could have it. The verb "Format" tells me that the results are for display only, so I wouldn't expect to be able to do something with the objects after formatting them.

  • #15281

    Vern Anderson
    Participant

    His legacy is about so much more than dead puppies, just saying!

    -VERN

You must be logged in to reply to this topic.