First things first: Write-Host
has few legitimate uses in general - do not use it to output data: Write-Host
's output goes straight to the console and can therefore neither be sent through the pipeline, nor captured, nor redirected (loosely speaking; for the full story, see the last section of this answer of mine).
If your intent is to print a friendly representation of an object for display only, use Out-Host
rather than Write-Host
, because the latter does not apply PowerShell's usual, friendly output formatting.
$DataFileContent | Out-Host # print object *for display only*
If your intent is to output data, you can rely on PowerShell's implicit output behavior, which sends any output that is not explicitly captured or redirected to the success output stream, which by default prints to the console using friendly formatting:
$DataFileContent # same as: Write-Output $DataFileContent
Explicit use of Write-Output
is rarely needed. A notable exception is if you want to output a collection as a single object, using -NoEnumerate
.
Note: Instances of [xml]
happen to have only semi-friendly default output formatting that provides no insight into the document structure; to see the latter, access the .OuterXml
property, though you may additionally want to apply pretty-printing, which requires more work.
Write-Host
output differs from Out-Host
and implicit / Write-Output
output that prints to the console as follows:
Instead of applying PowerShell's default output formatting, Write-Host
essentially calls the .ToString()
method on its input, which results in different, often near-useless representations - with the notable exception of string input.
Additionally, Write-Host
enumerates the input objects, if applicable, so that a given input object's elements are individually stringified.
An input object is considered enumerable if it implements the [System.Collections.IEnumerable]
interface.
This enumeration happens recursively, which in the case of [xml]
instances results in output that is not only useless, but also confusing - see below.
[xml]
instances, which represent XML documents, are enumerable, and so are their enumerated elements (XML nodes), recursively.
Write-Host
recursively enumerates the top-level nodes of the document - which is either the root element alone, or, additionally, if present, the XML declaration - and invariably hits one or more leaf nodes, whose enumeration is invariably empty and yields the empty string.
Given that Write-Host
separates the stringification of its (enumerated) input objects with a space each, you either get the empty string or one or more spaces as the overall output.