Again: do not use write-host cmdlet! This isn’t something reckless, but there’s a good explanation about this statement. First, if you use Powershell, you need to understand a basic concept:
Powershell is about automation
This means that Powershell scripts need to be write thinking about automation. And for this reason, Write-Host does not have nothing in common with automation. To understand why, we need to investigate on how Write-Host works. To get started, Powershell has many message streams:
To write inside these streams, we can use the following cmdlets (based on the previous list):
So, Write-Host is not related to any stream! This means that Write-Host log directly onto the console. And this is the only reason you should use it. If you need to create a script that is interactive or is ran into the console manually, then we could use it. Jeffrey Snover, Powershell’s father, in the article linked at the bottom, suggests to use it only in throw away scripts. Otherwise, is suggested to use Write-Output. Instead, if we would like to let the user choose what information he wants to see, you could use Write-Verbose.
Following, an example to understand how Write-Host works:
Inside log.txt file we can find just the output of Write-Output. Of course, we can cheat and use Write-Messages *> C:\Temp\log.txt. This way, we use redirection operator to force all outputs to be in the file (note: I’ve tested this only in PS 5). But if you want my 2 cents: don’t do it! Besides, also Write-Output can write in the console! The main difference is that information will be send through stream (as object) and could be used through piping. If Write-Output is the last cmdlet, then information will be sent to the console. One missing feature could be the output formatting, but using a bit of creativity we can use the following cmdlet (the idea came from Stack-Overflow: http://stackoverflow.com/questions/4647756/is-there-a-way-to-specify-a-font-color-when-using-write-output):
Write-Host considered harmful: http://www.jsnover.com/blog/2013/12/07/write-host-considered-harmful/