I just don’t understand why this is happening. If I create two separate new PSObjects, they seem to affect each other. For example, with this code:
$o1 = new-object psobject
$o1 | add-member noteproperty abc 123
$o2 = new-object psobject
$o2 | add-member noteproperty def 456
write-output $o1
write-output $o2
I would expect to see output for both abc and def, yet I only get abc:
abc
---
123
If I use write-host instead, like this:
write-host $o1
write-host $o2
Then it shows output like this:
@{abc=123}
@{def=456}
So according to write-output, $o2 is null/empty, but write-host says that’s a lie.
What’s going on?
If you run the sample lines from the console, you will see no issue. If you run them all in a script you will see an issue. This is because Out-Default (which is at the end of every pipeline) bases its formatting decisions on the first object it sees. So you need to make sure that you are not output more than one object type. Or if you are, you need to create some formatting rules that handle all the object types (like what is done for directory and file output from Get-ChildItem).