Edit: I think I figured out the answer. I will update this when I can better explain.
[PSCustomObject]@{keyA1="value"} ; [PSCustomObject]@{keyB1="value" ; keyB2="value"}
Output:
keyA1
----
value
Suppresses properties from second object.
[PSCustomObject]@{keyA1="value"} ; [PSCustomObject]@{keyA1="value" ; keyB2="value"}
Output:
keyA1
value
value
Combines like property names, even though they are different objects. Suppresses keyB2 property.
(1).foreach{[PSCustomObject]@{key1="value"} ; [PSCustomObject]@{keyA1="value" ; keyA2="value"} | Out-Host}
Output:
keyA1 keyA2
value value
key1
value
Output of the second object displayed before the first object when the second object is piped to Out-Host.
What are the rules that govern these behaviors?
In my actual code I am running into a scenario where XMLElement objects that are returned from Invoke-Restmethod are overwriting each other, but only if an XMLElement object that contains multiple properties is used before one that contains only a single property.
More details:
https://stackoverflow.com/questions/62025057/console-output-suppressed-only-for-second-invocation-of-invoke-restmethod-based
Sources:
https://devblogs.microsoft.com/powershell/how-powershell-formatting-and-outputting-really-works/
Out-Default looks at the FIRST OBJECT IN THE STREAM to determine how many properties the object has 5 or more properties, it send the ENTIRE STREAM to Format-List, otherwise it sends the ENTIRE STREAM to Format-Table.When it sends the stream to Format-Table, that command needs to generate columns. It does this by looking at the properties of the FIRST OBJECT – those become the columns. If the first Object has 2 properties, you’ll get a 2 column table even if all the other objects in the stream have 10 properties.
It is interesting to note that if you and selected enough properties, Out-Default would have sent the object stream to Format-list and then each object would have all of its properties displayed.
Well that seems to describe the behavior perfectly.