Home » PowerShell Array of Arrays

PowerShell Array of Arrays

In PowerShell, an array of arrays also known as a “jagged array” or a “multi-dimensional array”, can be created using several methods.

The following examples show how to create, access, and manipulate an array of arrays in PowerShell.

Create an Array of Arrays Using the Assignment Operator

You can create an array of arrays using the following PowerShell script.

# create an array of arrays
$arrayOfArrays =  @(
@(1,3,5),
@(7,9,11),
@(2,4,6)
)

# Output the array of arrays
Write-Host $arrayOfArrays

Output:

1 3 5
7 9 11
2 4 6

In this example, we define the variable $arrayOfArrays that stores the array of arrays.

We then use the Write-Host cmdlet to write the output of an array of arrays.

Create an Array of Arrays Using the Sub-Array

You can create an array of arrays by initializing each sub-array and combining them into a parent array.

# create an array of arrays using sub-array
$array1 = @(1,3,5)
$array2 = @(7,9,11)
$array3 = @(2,4,6)

# combine sub-array to create array of arrays
$arrayOfArrays = @($array1, $array2, $array3)

# output the array of arrays
Write-Host $arrayOfArrays

Output:

1 3 5
7 9 11
2 4 6

In this example, we define a variable $array1, $array2, and $array3 that contains the sub-array.

We then combine $array1, $array2, and $array3 to create a parent array and store them in the $arrayOfArrays variable.

Finally, using the Write-Host cmdlet, it prints the array of arrays.

Accessing the Elements in an Array of Arrays

You can access elements in an array of arrays using nested indices [][].

# access the elements in the array of arrays

Write-Output $arrayOfArrays[0]    #Output: 1,3,5
Write-Output $arrayOfArrays[1][2] #Output: 11
Write-Output $arrayOfArrays[2][1]  #Output: 4

In this example, the $arrayOfArrays[0] accesses the first array within the array of arrays.

The $arrayOfArrays[1][2] accesses the third element in the second array within the array of arrays.

The $arrayOfArrays[2][1] accesses the second element in the third array within the array of arrays.

Looping Through an Array of Arrays

You can use the nested loops to iterate through the elements of an array of arrays in PowerShell.

# Loop through the array of arrays
foreach ($array in $arrayOfArrays) {
    foreach ($element in $array) {
        Write-Output $element
    }
}

Output:

1
3
5
7
9
11
2
4
6

In this example, we use the nested foreach loop to iterate through the array of arrays stored in the variable $arrayOfArray.

Adding Elements in an Array of Arrays

To add elements to an existing array within an array of arrays, you can do it using the index. Here’s how you can do it in PowerShell.

# Access the second array and add elements to it
$arrayOfArrays[1] += 10, 11, 12

Write-Host $arrayOfArrays[1]

Output:

7 9 11 10 11 12

In this example, the $arrayOfArrays accesses the second array within the array of arrays, which is @(7,9,11), then uses += to add new elements 10,11,12 to that array.

We then use the Write-Host cmdlet to print the second subarray.

You can also add the elements in the sub-array as well as the parent array.

# Add a new sub-array to the array of arrays
$arrayOfArrays += @(8,10, 12)

Write-Host $arrayOfArrays

Output:

1 3 5
7 9 11
2 4 6
8 10 12

In this example, we add a new sub-array to the array of arrays in PowerShell.

Removing Elements In an Array of Arrays

To remove elements from an existing array within an array of arrays, you can do it using the index. Here’s how you can do it in PowerShell.

# Access the second array and remove elements from it
$arrayOfArrays[1] = $arrayOfArrays[1] | Where-Object { $_ -ne 7 }

Output:

1 3 5
9 11 10 11 12
2 4 6

In this example, the $arrayOfArrays[1] accesses the second array within the array of arrays and pipes the output to the Where-Object cmdlet to filter out unwanted elements from an array.

Updating Elements in an Array of Arrays in PowerShell

You can update elements in the array of arrays by accessing them using their indices.

# create an array of arrays
$arrayOfArrays =  @(
@(1,3,5),
@(7,9,11),
@(2,4,6)
)
# Update an element in a sub-array
$arrayOfArrays[1][0] = 8

# Output the updated array of arrays

Write-Host $arrayOfArrays

Output:

1 3 5
8 9 11
2 4 6

In this example, the $arrayOfArrays[1][0] accesses the first element in the second array within the array of arrays and updates the value to 8.

We then use the Write-Host command to output the array of arrays in PowerShell.

Finding Specific Elements in an Array of Arrays

You can find the specific elements in an array of arrays using nested loops.

# create an array of arrays
$arrayOfArrays =  @(
@(1,3,5),
@(7,9,11),
@(2,4,6)
)

# Find the specific element
$searchItem = 11
foreach ($subArray in $arrayOfArrays) {
    if ($subArray -contains $searchItem) {
        Write-Output "Found $searchItem in sub-array: $subArray"
    }
}

Output:

Found 11 in sub-array: 8 9 11

In this example, the $arrayOfArrays variable stores the array of arrays. The $searchItem variable stores the element that we want to search in an array of arrays.

We then use the foreach loop to find the specific element $searchItem in the $arrayOfArray using the condition specified in the if statement.

Finally, we use the Write-Output cmdlet to print the element found.

Conclusion

I hope the above article on an array of arrays in PowerShell is helpful to you.

You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.