It seems that calling the Powershell script block (by calling the .Invoke() method) always creates a collection. In particular, a set of types
System.Collections.ObjectModel.Collection`1[[System.Management.Automation.PSObject, System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]
Even calling an empty block script ( {}.Invoke() ) returns a collection. Calling the same script block using the call operator ( & ) gives the expected return (both a scalar and [object[]] ).
This is convenient if you need a collection instead of an array, but it seems to be the opposite.
Does anyone know why he behaves this way?
Edit:
I knew that there are two different calls, .Invoke() and .InvokeReturnAsIs() from reading the language specification. This is the first time I noticed.
I just do not understand the reasons underlying the agreement on the names and methods of mechanics. If you look at the documentation, I would think that this is the default invocation method - this is not what is used when the script script is called in Powershell. It looks like .InvokeReturnAsIs() just returns a stream of objects, and then Powershell wraps it in an array of objects if there is more than one object, like a scalar if there is only one object, or creates a null object if it isn't there, as if there implicit pipeline. Using .Invoke() returns the collection, always, and Powershell leaves it as a collection.
source share