There is only one rule regarding the expansion of pipeline elements: all arrays and collections written in the pipeline always expand to the elements (“expanded one level down” or “expanded in a non-recursive manner” will be a more correct statement, but for simplicity we will not consider nested arrays).
There is still the option to override this behavior using the unary comma operator:
$a = 1,2 $a | ForEach-Object{ $_.GetType() } IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Int32 System.ValueType True True Int32 System.ValueType ,$a | ForEach-Object{ $_.GetType() } IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Object[] System.Array
In the second case, the Powershell pipeline engine expanded $a , but then the result was returned to the array by the operator,.
Regarding the ConvertFrom-Json , I personally believe that its observed behavior is more predictable, since it allows us to collect JSON arrays in general by default. If you are interested in the details, the Get-WrappedArray in the code below mimics the behavior of ConvertFrom-Json :
function Get-WrappedArray { Begin { $result = @() } Process { $result += $_ } End { ,$result } } $a | Get-WrappedArray | ForEach-Object{ $_.GetType() } IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Object[] System.Array $b = $a | Get-WrappedArray $b | ForEach-Object{ $_.GetType() } IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Int32 System.ValueType True True Int32 System.ValueType
source share