bitbonk
bitbonk

Reputation: 49629

Select-Object join behavior in scripts

In this post some is surprised that the following example doesn't work properly when run in a script

get-process | select-object cpu, name
dir | Select-Object name, length

When I put this in a script the second command doesn't show the length

And the answer to this:

PowerShell is joining both outputs. You could pipe the first output to Format-Table [-auto] if you don't mind the format. Alternatively, you can separate the first output from the following formatted output by piping it to Out-Default, Out-Host, Out-string or Write-host

Now my question is why is it joining the output of those two seemingly unrelated commands and why doesn't this happen in the interactive console? They are not connected with a pipe. How does this work?

It seems that I still haven't quite understood basic concepts about piping in scripts.

Upvotes: 0

Views: 387

Answers (1)

mjolinor
mjolinor

Reputation: 68301

See if this helps: blogs.msdn.com/b/powershell/archive/2006/04/30/586973.aspx

This issue isn't really the pipeline, but the default console formatting

Upvotes: 1

Related Questions