PowerShell Multidimensional Arrays

Do you want to know about multidimensional arrays in PowerShell? In this tutorial, I will explain everything about PowerShell multidimensional arrays. Like how to create them and how to manipulate them in PowerShell.

What is a Multidimensional Array in PowerShell?

A multidimensional array in PowerShell is an array that contains one or more arrays as its elements. These arrays can be considered rows and columns, similar to a table or a spreadsheet. In PowerShell, multidimensional arrays are also known as jagged arrays because the inner arrays can be of different sizes.

Create Multidimensional Arrays in PowerShell

To create a multidimensional array in PowerShell, you define an array and then define arrays within it. PowerShell supports arrays of one or more dimensions, with each dimension having zero or more elements.

Example 1: Creating a Two-Dimensional Array

Here is one example of how you can create a two-dimensional array in PowerShell.

# Create an array with two arrays as elements
$multiArray = @( 
    @(1, 2, 3), # First row
    @(4, 5, 6)  # Second row
)

In the example above, we have created a two-dimensional array with two rows and three columns.

Example 2: Creating a Three-Dimensional Array

Here, is a PowerShell script to create a three-dimensional array.

# Create an array with two arrays, each containing two more arrays
$multiArray = @( 
    @( 
        @(1, 2), 
        @(3, 4)
    ),
    @( 
        @(5, 6), 
        @(7, 8)
    )
)

Here, we have a three-dimensional array, which can be visualized as two layers of two rows and two columns each.

Access Elements in a PowerShell Multidimensional Array

To access elements in a multidimensional array in PowerShell, you specify the index of each dimension separated by commas.

Example: Accessing Elements

Here is an example of how to access elements of a multidimensional array in PowerShell.

# Access the first element in the first row
$element = $multiArray[0][0]

# Access the second element in the second row
$element = $multiArray[1][1]

Here is the full PowerShell script:

# Create an array with two arrays, each containing two more arrays
$multiArray = @( 
    @( 
        @(1, 2), 
        @(3, 4)
    ),
    @( 
        @(5, 6), 
        @(7, 8)
    )
)

# Access the first element in the first row
$element0 = $multiArray[0][0]
$element0
# Access the second element in the second row
$element1 = $multiArray[1][1]
$element1

In PowerShell, array indices are zero-based, meaning the first element is at index 0.

Once you run the PowerShell script using VS code, you can see the output in the screenshot below.

PowerShell Multidimensional Arrays

ModifyElements in a Multidimensional Array in PowerShell

Modifying an element in a multidimensional array in PowerShell is similar to accessing it. You specify the indices and then assign a new value.

Example: Modifying Elements

# Change the first element in the first row to 10
$multiArray[0][0] = 10

# Change the second element in the second row to 20
$multiArray[1][1] = 20

Add and Remove Elements from a PowerShell Multidimensional Array

PowerShell does not natively support resizing arrays, including multidimensional ones. However, you can use the ArrayList class or create a new array if you need to add or remove elements dynamically.

Example: Using ArrayList

# Create an ArrayList
$arrayList = New-Object System.Collections.ArrayList

# Add an array to the ArrayList
$arrayList.Add(@(1, 2, 3))

# Add another array
$arrayList.Add(@(4, 5, 6))

# Remove the first array
$arrayList.RemoveAt(0)

The ArrayList class provides methods such as Add, Remove, and RemoveAt to manage elements dynamically.

Looping Through PowerShell Multidimensional Arrays

To loop through a PowerShell multidimensional array, you can use nested loops. The outer loop iterates over the first dimension, and the inner loop iterates over the second dimension.

Example: Looping Through a Two-Dimensional Array

foreach ($row in $multiArray) {
    foreach ($element in $row) {
        Write-Host $element
    }
}

Once you execute the PowerShell script, this will print each element of the two-dimensional array to the console.

Conclusion

Multidimensional arrays in PowerShell are a powerful way to organize and manipulate complex collections of data. In this PowerShell tutorial, I have explained how to create a multidimensional array in PowerShell and also:

  • Create Multidimensional Arrays in PowerShell
  • Access Elements in a PowerShell Multidimensional Array
  • ModifyElements in a Multidimensional Array in PowerShell
  • Add and Remove Elements from a PowerShell Multidimensional Array
  • Looping Through PowerShell Multidimensional Arrays

You may also like: