What is write-host?

Write-host is one of those easy cmdlets to tell the user what is going on in a script. Kind of funny, isn’t it? We’re writing scripts to automate things, but we still want people to know what is going on…

One of the major issues with write-host before PowerShell 5 is that it simly puts a text on the console. There is no way for the end-user to save, redirect, or ignore it.

So in essence you would not be able to pass the text you’re writing on to any other pipelines, making write-host just a very dumb tool that does not fit the general flexible environment of PowerShell.

Write-output

A better alternative to using write-host is write-output. It is able to be saved, redirected, or ignored if so desired by the end-user, and generally is better at handling objects!

The downside, of course there always has to be a downside, is that it doesn’t allow you to write beautifully colored messages… Then again, there are ways around that 🙂

write-output and colored messages

One way (I’m certain there are more!) would be to change the $host.UI.RawUI.ForegroundColor property to the color you want displayed. Alas, this changes the setting for the entire console, so you would have to set it back to the previous color after running your write-output command.