Write-Host and Write-Output

Good to remember:

  1. Write-Host converts the object to a string representation and then outputs it to the host (console). There’s two things to keep in mind here: (1) the output bypasses any pipelines etc and is sent directly to the host; (2) the object is converted to a string representation, using the toString() method (which is present in all classes), and so the string representation may not necessarily be what you expect.

    Case in hand:

  2. Write-Output is better in the sense that (1) it only passes the output to the next step of the pipeline (or to the host if we are at the end of the pipeline) and (2) no conversion happens, the object is output and formatted as it is.

    To give an example with the previously created table:

    This is same as if you were to output the object directly:

    As previously mentioned the formatting of such output depends the ps1xml defintions for that object.