In this tutorial, I will explain how to create and use PowerShell ArrayList with some examples for efficient data management. As a PowerShell developer or administrator when you start using ArrayList over an Array, it will improve your script’s performance. I will also show the difference between an array and an ArrayList.
What is a PowerShell ArrayList?
An ArrayList in PowerShell is a dynamic collection that allows you to store and manipulate a list of objects. Unlike a traditional array, an ArrayList can grow or shrink in size as needed, making it more flexible and efficient when dealing with large datasets or unknown quantities of data.
An ArrayList is a part of the System.Collections
namespace. Unlike arrays, ArrayLists are dynamic; they can grow and shrink as needed, which makes them more flexible for scenarios where the size of the collection might change.
Check out Access Array of Objects in PowerShell
Create an ArrayList in PowerShell
To create an ArrayList in PowerShell, you can use the New-Object
command or directly call the constructor of the System.Collections.ArrayList
class. Here’s an example:
# Creating an ArrayList using New-Object
$statesArrayList = New-Object System.Collections.ArrayList
# Creating an ArrayList using the constructor
$statesArrayList = [System.Collections.ArrayList]::new()
In this example, we create a new ArrayList called $employees
that will store employee names.
Add Elements to an ArrayList
Adding elements to an ArrayList is a breeze. You can use the Add()
method to append items to the end of the list. Let’s add some employee names to our $employees
ArrayList:
$statesArrayList = [System.Collections.ArrayList]::new()
# Adding elements to the ArrayList
$statesArrayList.Add("California")
$statesArrayList.Add("Texas")
$statesArrayList.Add("Florida")
$statesArrayList.Add("New York")
Now, our ArrayList contains all the above four state names.
Access Elements in an ArrayList
To access individual elements in an ArrayList, you can use the index notation just like with regular arrays. ArrayList indices start at 0. Here’s an example:
# Accessing elements
$firstState = $statesArrayList[0] # California
$secondState = $statesArrayList[1] # Texas
You can also use the Get()
method to retrieve an element at a specific index:
$statesArrayList.Get(2) # Output: Florida
Check out Remove the First and Last Item in an Array in PowerShell
Remove Elements from an ArrayList
Removing elements from an ArrayList is just as simple. You can use the Remove()
method to remove a specific object or the RemoveAt()
method to remove an element at a specific index. Let’s remove “Florida” from our $
statesArrayList ArrayList:
# Removing an element by value
$statesArrayList.Remove("Florida")
# Removing an element by index
$statesArrayList.RemoveAt(2) # Removes "New York"
Now, the ArrayList contains only “California” and “Texas”.
Check if an Element Exists in An ArrayList
To check if an element exists in an ArrayList, you can use the Contains()
method. It returns $true
if the element is found and $false
otherwise. Here’s an example:
$statesArrayList.Contains("Texas") # Output: True
$statesArrayList.Contains("New York") # Output: False
Read Split an Array into Smaller Arrays in PowerShell
Iterate Over a PowerShell ArrayList
Iterating over an ArrayList is simple using a foreach
loop in PowerShell. Let’s print all the employee names in our ArrayList:
# Creating the ArrayList
$statesArrayList = [System.Collections.ArrayList]::new()
# Adding states
$statesArrayList.Add("California")
$statesArrayList.Add("Texas")
$statesArrayList.Add("Florida")
$statesArrayList.Add("New York")
foreach ($state in $statesArrayList) {
Write-Host $state
}
Output:
California
Texas
Florida
New York
Here is the output you can see in the screenshot below:

Convert an Array to an ArrayList
If you have an existing array and want to convert it to an ArrayList, you can use the AddRange()
method. Here’s an example and the complete PowerShell:
$states = "California", "Texas", "Florida", "New York"
$statesList = New-Object System.Collections.ArrayList
$statesList.AddRange($states)
Read PowerShell try catch with examples
PowerShell ArrayList Example
Let me show you an example where we can use an ArrayList which can be beneficial over an Array in PowerShell.
Suppose you’re working on a script to process a large number of user accounts from a CSV file. You need to filter out specific accounts based on certain criteria and perform actions on the remaining accounts.
$accounts = New-Object System.Collections.ArrayList
# Read user accounts from CSV file
$csvAccounts = Import-Csv -Path "C:\MyWorkFolder\user_accounts.csv"
# Add user accounts to the ArrayList
$accounts.AddRange($csvAccounts)
# Filter out accounts based on criteria
for ($i = $accounts.Count - 1; $i -ge 0; $i--) {
if ($accounts[$i].Department -ne "IT" -or $accounts[$i].Status -eq "Inactive") {
$accounts.RemoveAt($i)
}
}
# Perform actions on the remaining accounts
foreach ($account in $accounts) {
# Execute desired actions
Write-Host "Processing account: $($account.Name)"
}
In this example, we leverage an ArrayList to store user accounts read from a CSV file. We filter out accounts based on department and status criteria by iterating through the ArrayList in reverse order and removing the accounts that don’t meet the criteria. Finally, we perform actions on the remaining accounts.
Using an ArrayList in this scenario allows for efficient removal of elements without the need to create a new array each time an account is filtered out.
Read PowerShell Copy Item examples
Why Use ArrayList Over Array?
Here are two important reasons why we prefer to use an ArrayList over an Array in PowerShell.
Flexibility and Performance
When dealing with large datasets, using an ArrayList can significantly improve performance compared to traditional arrays. ArrayList offers better performance when adding, removing, or inserting elements, as it dynamically resizes itself without the need to create a new array.
However, it’s important to note that accessing elements in an ArrayList is slightly slower compared to a fixed-size array. If you have a fixed number of elements and don’t require the dynamic resizing capabilities, using a regular array might be more efficient.
Ease of Use
With ArrayLists, you don’t need to worry about the size of your collection. You can add and remove elements without having to create a new array each time.
PowerShell ArrayList is useful that offers flexibility, dynamic resizing, and improved performance when working with collections of objects. In this tutorial, I explained how to create and use an ArrayList in PowerShell with a few examples. I have also shown how to add element, remove elements, access elements or iterate over a PowerShell ArrayList with examples.
You may also like:
- Loop Through an Array in PowerShell
- Filter An Array with Multiple Conditions in TypeScript
- Filter Empty Strings from an Array in TypeScript
I am Bijay a Microsoft MVP (10 times – My MVP Profile) in SharePoint and have more than 17 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com
This “tutorial” first asks the question “What is PowerShell Arraylist?” It’s immediately followed by, “We can use an ArrayList…” Why not immediately follow a question with an answer??? This makes a tutorial hard to follow and not very useful.
Hi Fred, Thanks for your comment, I will try to improve. But hopefully, the content will be helpful to a few people who are new to PowerShell Arraylist.
It looks like your article has been republished elsewhere if you didn’t already know.
Thanks for letting me know. I complained and then removed all the articles. Thanks again.