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, 5On 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
$arrayIn 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:

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
$arrayWhen 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
$arrayThis 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:
- How To Add Multiple Values To Array In PowerShell?
- How to Check an Array for Two Conditions in PowerShell?
- How to Read JSON File into Array in PowerShell?
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.