Forum Discussion
Get-ChildItem | Write-Host
Hi Bosjabouter​,
Write-Host isn't behaving differently. In fact, this actually has nothing to do with Write-Host at all.
While I can't go so far as to debug the compiled .dll that Get-ChildItem is shipped in, the behaviour you're observing (which I can also reproduce across both Windows PowerShell and PowerShell) can be explained by the underlying .NET type, and the ToString() method in particular:
As noted in the above documentation, ToString() returns the string provided to the class' constructor.
What the key events look like in reverse chronology is:
- Write-Host (not that it's specific to Write-Host) calls [objectType].ToString();
- ToString() returns the path provided to the constructor;
- The Get-ChildItem commandlet creates a new instance of [objectType], passing in a string literal to the constructor. This is where your observed behaviour originates from;
- The Get-ChildItem commandlet enumerates file system objects.
Leaving Write-Host out of the equation entirely, you can see the behaviour persists even when calling collection-specific methods:
What this comes down to is it's an oversight (bug, if you must) within the Get-ChildItem commandlet itself, where it's what's producing the inconsistent output. But then referring back to the class documentation above, this is what's being acknowledged by the remarks where if you want consistent output, refer to the Name or FullName attributes explicitly:
Cheers,
Lain