How to Convert an ArrayList to an Array in PowerShell?

There might be a situation where you might be required to convert an ArrayList to an array in PowerShell. In this PowerShell tutorial, I will show you how to convert an ArrayList to an array in PowerShell using various methods.

To convert an ArrayList to an array in PowerShell, use the ToArray() method on the ArrayList instance. For instance, $array = $arrayList.ToArray() will create a standard array from the ArrayList. This method is a straightforward way to transition from a dynamically-sized collection to a fixed-size array.

Introduction to Arrays and ArrayLists in PowerShell

Let’s first understand what arrays and ArrayLists are in PowerShell.

An array is a fundamental data structure in PowerShell that stores a fixed-size sequential collection of elements of the same type. Arrays are used to store multiple values in a single variable, which their index can access. Here’s a simple example of an array in PowerShell:

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

On the other hand, an ArrayList is a dynamic object that can hold a collection of items. Unlike arrays, ArrayLists can grow and shrink in size dynamically, and they can hold items of different types.

ArrayLists come from the .NET Framework and are not native to PowerShell, but they can be very useful when you need a collection that can change size. Here’s how you can create an ArrayList in PowerShell:

$arrayList = New-Object System.Collections.ArrayList
$arrayList.Add(1)
$arrayList.Add('two')
$arrayList.Add(3.0)

Why Convert an ArrayList to an Array?

You might need to convert an ArrayList to an array in PowerShell for several reasons. For example:

  • You may have a function that only accepts arrays as input.
  • Arrays are faster to access in a loop as they are a fixed size.
  • Some cmdlets or methods may require an array instead of an ArrayList.

How to Convert an ArrayList to an Array in PowerShell

Now, let’s look at how you can convert an ArrayList to an array. There are a few methods to do this, and we’ll explore each one with examples.

Method 1: Using the ToArray() Method

The most straightforward way to convert an ArrayList to an array in PowerShell is by using the ToArray() method. This method is a member of the ArrayList class and converts the entire ArrayList into an array. Here’s how you can use it:

# Create an ArrayList and add some items
$arrayList = New-Object System.Collections.ArrayList
$arrayList.Add('PowerShell')
$arrayList.Add(101)
$arrayList.Add($true)

# Convert the ArrayList to an Array
$array = $arrayList.ToArray()

# Display the Array
$array

In this example, $array will now be an array containing the elements that were in $arrayList.

Once you execute the script using any editor like VS code or Windows PowerShell ISE, you can see the output like the screenshot below:

Convert an ArrayList to an Array in PowerShell

Method 2: Using PowerShell Casting

Another way to convert an ArrayList to an array is by using PowerShell’s casting feature, where you can cast the ArrayList to the desired array type. Here’s an example:

# Create an ArrayList and add some items
$arrayList = New-Object System.Collections.ArrayList
$arrayList.Add(10)
$arrayList.Add(20)
$arrayList.Add(30)

# Cast the ArrayList to an Array
[array]$array = $arrayList

# Display the Array
$array

When you cast like this, PowerShell will create a new array of the same type as the items in the ArrayList.

Method 3: Using the @() Array Subexpression Operator

You can also use the array subexpression operator @() to convert an ArrayList to an array in PowerShell. This operator always returns an array, even if you use it with a single item. Here’s an example:

# Create an ArrayList and add some items
$arrayList = New-Object System.Collections.ArrayList
$arrayList.Add('a')
$arrayList.Add('b')
$arrayList.Add('c')

# Use the array subexpression operator to convert to an Array
$array = @($arrayList)

# Display the Array
$array

This method is especially useful if you want to ensure that the result is always an array, even if the ArrayList is empty or contains only one item.

Conclusion

In PowerShell, converting an ArrayList to an array can be accomplished in several ways, including using the ToArray() method, casting, and the array subexpression operator. Each method has its own use cases and can be chosen based on the specific needs of your script or function.

I hope now you have a complete idea of how to convert an ArrayList to an array in PowerShell.

You may also like: