Dynamic Arrays in PowerShell [With Examples]

In this PowerShell tutorial, I will explain to you what is a dynamic array in PowerShell. And then how to work with PowerShell dynamic arrays.

A dynamic array in PowerShell is not a specific type of array but rather refers to the ability to modify an array after its creation, such as adding or removing items. While PowerShell’s arrays are fixed in size, you can mimic dynamic behavior by using an ArrayList or a List object, which allows for dynamic resizing and provides methods for adding and removing elements efficiently.

What is a Dynamic Array in PowerShell?

In programming, an array is a collection of items stored at contiguous memory locations. A dynamic array in PowerShell, unlike a static array, can grow and shrink in size dynamically during runtime. This means you can add or remove items from the array without having to specify the size of the array when it’s declared.

Create a Dynamic Array in PowerShell

In PowerShell, you don’t need to declare the size of an array because it’s dynamic by default. Here is how you can create a dynamic array in PowerShell:

# Create an empty array
$dynamicArray = @()

This creates an empty array that you can add elements to dynamically.

Add Elements to a Dynamic Array in PowerShell

To add elements to a dynamic array in PowerShell, you can use the += operator. Here’s an example:

# Create an empty dynamic array
$dynamicArray = @()

# Add elements to the array
$dynamicArray += 'PowerShell'
$dynamicArray += 'is'
$dynamicArray += 'awesome'

# Display the array
$dynamicArray

This will output:

PowerShell
is
awesome

You can check out the output in the screenshot below after I executed the script using VS code.

powershell dynamic arrays

Remove Elements from a Dynamic Array

To remove an element from a dynamic array in PowerShell, you can use the Remove method if you have a collection that supports this method or filter the array to exclude the element you want to remove:

# Assuming you have the following array
$dynamicArray = 'PowerShell', 'is', 'awesome'

# Remove the element 'is'
$dynamicArray = $dynamicArray | Where-Object { $_ -ne 'is' }

# Display the array
$dynamicArray

This will output:

PowerShell
awesome

Modifying Elements in a Dynamic Array in PowerShell

To modify an element in a dynamic array in PowerShell, you can access it by its index and assign a new value:

# Assuming you have the following array
$dynamicArray = 'PowerShell', 'is', 'awesome'

# Change 'awesome' to 'amazing'
$dynamicArray[2] = 'amazing'

# Display the array
$dynamicArray

This will output:

PowerShell
is
amazing

Looping Through a PowerShell Dynamic Array

You can loop through the elements of a dynamic array in PowerShell using a foreach loop:

# Create an array with some elements
$dynamicArray = 'PowerShell', 'is', 'awesome'

# Loop through the array and print each element
foreach ($element in $dynamicArray) {
    Write-Host $element
}

Advanced Operations with PowerShell Dynamic Arrays

You can perform more complex operations on dynamic arrays in PowerShell, such as sorting, filtering, and mapping. Here are some examples:

Sorting an Array

Here is an example of how to sort a PowerShell dynamic array.

# Create an array with unordered elements
$dynamicArray = 'banana', 'apple', 'cherry'

# Sort the array
$sortedArray = $dynamicArray | Sort-Object

# Display the sorted array
$sortedArray

This will output the array elements in alphabetical order.

Filtering an Array

Here is an example of how to filter an array in PowerShell.

# Create an array with a mix of values
$dynamicArray = 1, 'two', 3, 'four', 5

# Filter the array to include only integers
$integersOnly = $dynamicArray | Where-Object { $_ -is [int] }

# Display the filtered array
$integersOnly

This will output only the integer values from the array.

Mapping Over an Array

# Create an array with some numbers
$dynamicArray = 1, 2, 3, 4, 5

# Map over the array to create a new array of squared numbers
$squaredArray = $dynamicArray | ForEach-Object { $_ * $_ }

# Display the squared array
$squaredArray

This will output an array of the squared values of the original array.

Conclusion

Dynamic arrays in PowerShell provide the flexibility to store and manipulate collections of data without the need for predefined array sizes. With the ability to add, remove, and modify elements on the fly, dynamic arrays can greatly simplify your scripts and make them more efficient.

In this PowerShell tutorial, I have explained how to work with PowerShell dynamic arrays and the following points:

  • What is a Dynamic Array in PowerShell?
  • Create a Dynamic Array in PowerShell
  • Add Elements to a Dynamic Array in PowerShell
  • Remove Elements from a Dynamic Array
  • Modifying Elements in a Dynamic Array in PowerShell
  • Looping Through a PowerShell Dynamic Array

You may also like: