Jaa


How to create multiple or jagged arrays in Powershell

For me, it was not apparent to create multiple dimensional or jagged arrays in Powershell. I tried the following command, but it only made a single dimensional array:

$b = ((‘a’,’a’))

$b.Count

2

Bruce Payette gave me the answers for this basic question.

 

Here is how to create multiple arrays:

$x = new-object "int32[,]" 10,10

$x.rank

$x[0,5] = 13

$x[0,5]

13

And, here is how to create jagged arrays:

$a = (1,2,3),("a","b","c"),([char[]]"Hello")

$a[0]

1

2

3

$a[0][1]

2

$a[1][1]

b

$a[2][4]

o

$a[2][1..3]

Or..you can use unary comma operator as follows:

$a = ,(1,2)
$a.count
1
$a[0]
1
2
$a[0].count
2

Comments