It’s one of those messy things with powershell and functions.
There is a pretty good explanation in this post (a bit further down): understanding output
To get around your problem either use [void] or pipe the AppendChild statements to Out-Null.
Otherwise the output of those statements will be added to the output of the function.
Which will give you an array instead of an XML document.
Interesting, at aprox. what amount of statements/iterations did you notice the difference?
Haven’t investigated it myself so I guess I haven’t run into the problem, that I know of at least.
So in this particular instance, [void] and > $null worked the same, really. It seems to vary depending on iterations and what kind of things you’re discarding, especially with Out-Null. You’ll also get slightly different results if you repeat the test a few times, and it tends to average roughly in the order I mentioned, although pretty much any alternative to Out-Null works better than it does.
Interestingly, doing this actually works pretty quickly as well:
Out-Null -InputObject $Stuff
So it’s the pipeline that causes the overhead, more than the command. But if you’ve already got a pipeline spun up, I don’t think tacking on Out-Null instead of these other options would really slow things much, in that instance.
When I measure commands like that, I do it a little different to time each execution and average them out. Here are my result. The cmdlet is still slower than the others.