I\'ve recently started scripting in PowerShell and I\'m having a difficulty in sorting a 2D array by multiple columns.
The following code is working fine. I have a 2D ar
Sort-Object
does not return array. It return write to pipeline individual input items in sorted order.
PS> 7,3,8,5,1 | Sort-Object | ForEach-Object { Write-Host "Item: $_" }
Item: 1
Item: 3
Item: 5
Item: 7
Item: 8
As you can see, next command in pipeline ForEach-Object
see each individual item, but not array as whole.
Wrapping to array in $table1
case happens, because last command in pipeline wrote more than one item in pipeline. In $table2
case last command in pipeline wrote only one item, so no wrapping necessary there. If you want to always wrap pipeline results into array regardless of amount of results (zero, one or many), then you need to use array subexpression operator:
$Results = @( First-Command | Middle-Command | Last-Command )