PowerShell Array vs ArrayList [Complete Guide]

When working with collections of data in PowerShell, two common types are arrays and ArrayLists. Both serve the purpose of holding multiple items, but they have different characteristics and methods that can make one more suitable than the other, depending on the situation. In this post, we will look at the differences between arrays and ArrayLists in PowerShell and how they work.

Understanding Arrays in PowerShell

An array is a fixed-size, ordered collection of elements. Once an array is created in PowerShell, its size cannot be changed, which means you cannot add or remove items without creating a new array.

Creating an Array

To create an array in PowerShell, you simply assign multiple values to a variable, separated by commas.

$array = 1, 2, 3, 4, 5

Adding Elements to an Array

To add elements to an array, you have to create a new array by combining the existing array with the new elements.

$array = $array + 6

Removing Elements from an Array

Removing elements is not straightforward because you cannot directly remove an item from an array. You have to filter the array for the elements you want to keep.

$array = $array | Where-Object { $_ -ne 3 }

Accessing and Modifying Elements

Accessing elements in an array is done using the index (position of the element in the array).

$thirdElement = $array[2]

Modifying an element is similar:

$array[2] = 10

Understanding ArrayLists in PowerShell

An ArrayList, on the other hand, is a dynamic object that can grow and shrink in size. It is part of the System.Collections namespace in .NET, which PowerShell can access.

Creating an ArrayList

To create an ArrayList, you need to use the New-Object cmdlet.

$arrayList = New-Object System.Collections.ArrayList

Adding Elements to an ArrayList

Adding elements to an ArrayList is simple and does not require creating a new instance.

$arrayList.Add(1)
$arrayList.Add(2)

You can also add multiple elements at once:

$arrayList.AddRange(@(3, 4, 5))

Removing Elements from an ArrayList

ArrayLists have a Remove method that allows you to remove elements by specifying the actual element.

$arrayList.Remove(3)

You can also remove elements by index with the RemoveAt method.

$arrayList.RemoveAt(0)

Accessing and Modifying Elements

ArrayList elements are accessed and modified in the same way as array elements, using the index.

$thirdElement = $arrayList[2]
$arrayList[2] = 10

Performance Considerations

When choosing between an array and an ArrayList, one of the key considerations is performance. Arrays are generally faster when the size of the collection is known and does not change because they have a fixed size and do not require overhead for resizing. ArrayLists are more flexible and can be more efficient when you need to add or remove items frequently.

Examples

Let’s look at a few examples to see how arrays and ArrayLists work in practice.

Example 1: Adding Elements

# Array
$array = 1, 2, 3
$array = $array + 4

# ArrayList
$arrayList = New-Object System.Collections.ArrayList
$arrayList.AddRange(@(1, 2, 3))
$arrayList.Add(4)

Example 2: Removing Elements

# Array
$array = 1, 2, 3, 4, 5
$array = $array | Where-Object { $_ -ne 3 }

# ArrayList
$arrayList = New-Object System.Collections.ArrayList
$arrayList.AddRange(@(1, 2, 3, 4, 5))
$arrayList.Remove(3)

Example 3: Modifying Elements

# Array
$array = 1, 2, 3, 4, 5
$array[2] = 10

# ArrayList
$arrayList = New-Object System.Collections.ArrayList
$arrayList.AddRange(@(1, 2, 3, 4, 5))
$arrayList[2] = 10

Array vs. ArrayLists in PowerShell

To summarize the differences and similarities between arrays and ArrayLists in PowerShell, here is a comparison table:

FeatureArrayArrayList
TypeFixed-sizeDynamic
NamespaceBuilt-inSystem.Collections
Adding ItemsRequires creating a new arrayUse Add or AddRange methods
Removing ItemsRequires filtering the arrayUse Remove or RemoveAt methods
Accessing ElementsBy indexBy index
Modifying ElementsBy indexBy index
PerformanceFaster for fixed-size collectionsMore overhead, but better for frequent resizing

Conclusion

Arrays and ArrayLists in PowerShell have their own set of features and use cases. Arrays are great for static collections where the size doesn’t change, while ArrayLists offer more flexibility for dynamic collections. Understanding the differences and how to use each type effectively is crucial for writing efficient PowerShell scripts.

In this post, we’ve covered the basics of arrays and ArrayLists, with examples to help beginners understand how to work with these collections. Whether you choose an array or an ArrayList will depend on your specific needs, but now you have the knowledge to make an informed decision.

You may also like: