Search code examples
arrayspowershellmultidimensional-array

Why does this multidimension array output as list?


A list

$tags = 'a','b','c','d','e'

the process

$array = New-Object 'object[,]' 3,5

    $array[0,0] = $tags[0]
    $array[0,1] = $tags[1]
    $array[0,2] = $tags[2]
    $array[0,3] = $tags[3]
    $array[0,4] = $tags[4]

    $array[1,0] = $tags[0]
    $array[1,1] = $tags[1]
    $array[1,2] = $tags[2]
    $array[1,3] = $tags[3]
    $array[1,4] = $tags[4]

$array |Format-Table

The output

This should be 2 rows and 5 columns of data

enter image description here


Solution

  • To complement Santiago Squarzon general answer on a multidimension array output as list (as written in the title). Based on the content of your question (and your comments), I guess you actual not looking for a multidimension array but a list of PSCustomObjects (which is also most practical to work with in PowerShell):

    $tags = 'a','b','c','d','e'
    
    $array =
        [PSCustomObject]@{
            $tags[0] = 0
            $tags[1] = 1
            $tags[2] = 2
            $tags[3] = 3
            $tags[4] = 4
        },
        [PSCustomObject]@{
            $tags[0] = 5
            $tags[1] = 6
            $tags[2] = 7
            $tags[3] = 8
            $tags[4] = 9
        }
    

    The output

    "This should be 2 rows and 5 columns of data"

    $array | Format-Table
    
    a b c d e
    - - - - -
    0 1 2 3 4
    5 6 7 8 9
    

    For using a list of PSCustomObjects, see also: Powershell Multidimensional Arrays.