How to Add Values to a Multidimensional Array in PowerShell?

One of my team members recently asked to add values to a multidimensional array in PowerShell. There are various methods to do so. In this tutorial, I will explain how to add values to a multidimensional array in PowerShell. I will also show how to add values to 2-dimensional array in PowerShell.

To add values to a multidimensional array in PowerShell using indexes, you can directly assign values to specific positions within the array. First, initialize your array, for example, $array = @(@(1, 2), @(3, 4)). Then, to add a value to a specific position, such as the first row and third column, use $array[0] += 5.

Add Values to a Multidimensional Array in PowerShell

A multidimensional array in PowerShell is essentially an array of arrays. This structure allows you to store data in a grid-like format, which is useful for various applications, such as managing data tables or matrices.

Syntax to Create a Multidimensional Array

To create a multidimensional array in PowerShell, you can use the following syntax:

# Creating a 2x3 array (2 rows and 3 columns)
$array = @(@(1, 2, 3), @(4, 5, 6))

In this example, $array is a 2×3 array with two rows and three columns.

There are several methods to add values to a multidimensional array in PowerShell. Let me show you each method with examples.

Method 1: Using Indexes to Add Values

You can directly assign values to specific indexes in the multidimensional array in PowerShell. Here’s how:

# Initialize a 2x2 array
$array = @(@(1, 2), @(3, 4))

# Add a value to the first row, third column
$array[0] += 5

# Add a value to the second row, third column
$array[1] += 6

# Display the array
$array

In this example, we added the values 5 and 6 to the first and second rows, respectively. The += operator appends the value to the existing row.

I executed the above PowerShell script using VS code, and you can see the output in the screenshot below:

powershell add values to multidimensional array

Check out Array Contains in PowerShell

Method 2: Using the Add() Method

Another approach is to use the Add() method to append values to the multidimensional array. This method works well when dealing with custom objects or when you need to add entire rows or columns.

# Initialize an empty array
$array = @()

# Add rows to the array
$array += ,@(1, 2, 3)
$array += ,@(4, 5, 6)

# Display the array
$array

Here, we added two rows to the array using the += operator. Each row is represented as an array, and the comma before the array ensures it’s treated as a single element.

Here is the exact output you can see in the screenshot below:

add values to multidimensional array powershell

Check out Convert an Array to a String in PowerShell

Method 3: Using Custom Objects

Using custom objects can be beneficial for more complex data structures, such as when multiple properties need to be added.

# Initialize an empty array
$array = @()

# Define custom objects
$row1 = [PSCustomObject]@{Name = "John"; Age = 30; City = "New York"}
$row2 = [PSCustomObject]@{Name = "Jane"; Age = 25; City = "Los Angeles"}

# Add custom objects to the array
$array += $row1
$array += $row2

# Display the array
$array

In this example, we created custom objects with properties Name, Age, and City. We then added these objects to the array, allowing us to store more structured data.

Here is the exact output in the screenshot below:

Add Values to a Multidimensional Array in PowerShell

Method 4: Using Nested Loops

Nested loops can be useful for large multidimensional arrays or when you need to fill the array programmatically. Here is the complete PowerShell script.

# Initialize an empty array
$array = @()

# Define dimensions
$rows = 3
$columns = 3

# Populate the array using nested loops
for ($i = 0; $i -lt $rows; $i++) {
    $row = @()
    for ($j = 0; $j -lt $columns; $j++) {
        $row += $i * $columns + $j + 1
    }
    $array += ,$row
}

# Display the array
$array

This script creates a 3×3 array and populates it with sequential numbers using nested loops.

Check out Check if an Array is Empty in PowerShell

Add Values to a 2-Dimensional Array in PowerShell

Adding values to a 2-dimensional array in PowerShell can be done in various ways. Here, I’ll explain two effective methods: using indexes and using the += operator.

Method 1: Using Indexes to Add Values

This method involves directly assigning values to specific positions within the 2-dimensional array in PowerShell. Here’s a step-by-step example:

  • Initialize the array:
$array = @(@(1, 2), @(3, 4))
  • Add a value to a specific position:
# Add value 5 to the first row, third column
$array[0] += 5
# Add value 6 to the second row, third column
$array[1] += 6
  • Display the array:
$array

After running the above script, the array will look like this:

1 2 5
3 4 6

Method 2: Using the += Operator to Add Rows

This method is useful for adding entire rows to the 2-dimensional array in PowerShell. Here’s how:

  • Initialize an empty array:
$array = @()

Add rows using the += operator:

# Add the first row
$array += ,@(1, 2, 3)
# Add the second row
$array += ,@(4, 5, 6)

Display the array:

$array

After running the above script, the array will look like this:

1 2 3
4 5 6

By using these methods, you can efficiently add values to a 2-dimensional array in PowerShell.

Conclusion

In this tutorial, I explained how to add values to a multidimensional array in PowerShell using different methods such as direct indexing, the Add method, custom objects, or nested loops. You also learn how to add values to a 2-dimensional array in PowerShell.

I provided real examples for each method that helped you understand it better.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.