In this tutorial, we will discuss, what is a PowerShell ArrayList how to create and use ArrayList.
Also, we will see how to sort an ArrayList, remove items from an ArrayList, how to sort an ArrayList, and how to create an ArrayList from an array.
In my previous article, we discussed what is an array in PowerShell and how to create a PowerShell array from the CSV file.
What is PowerShell Arraylist
We can use an ArrayList to store a list of items in PowerShell.
Unlike array, arraylist’s length is not fixed, it can changed.
One difference between array and ArrayList is, An array is strongly types, that means array can store only specific type elements. On the other hand, ArrayList can store all the datatype values.
Create ArrayList in PowerShell
Now, we will see how to create an arraylist in PowerShell.
ArrayList is part of the System.Collections namespace within .NET. And we can create a new object of type System.Collections.ArrayList to create an ArrayList.
Below are the two ways to create an arraylist.
$demoarrayList = New-Object -TypeName 'System.Collections.ArrayList';
or
$demoarrayList = [System.Collections.ArrayList]::new()
This way, we can create arraylist of objects type.
You can see by calling the GetType() method.

How to create an empty ArrayList
We can also create an empty arraylist like below:
[System.Collections.ArrayList]$demoArrayList= @()
This will create an empty arraylist.
PowerShell ArrayList add
Now, we will see how to add item to arraylist.
We can use the Add() method to add an item to the arraylist.
$demoarrayList = New-Object -TypeName 'System.Collections.ArrayList';
$demoarrayList.Add("Bijay")
$demoarrayList.Add("Bhawana")
$demoarrayList.Add(100)
You can retrieve the arraylist elements by using the arraylist name like below:
$demoarrayList

How to Get ArrayList Item count
We can count the items from the ArrayList by using the Count property.
$demoarrayList.Count
You can see the output like below:

Sort ArrayList Items
We can easily sort arraylist using sort-object.
$demoarrayList | sort-object
You can see now the Arraylist is displaying in sorted order like below:

This is how we can sort a ArrayList.
PowerShell arraylist foreach
Now, we will see how to iterate ArrayList by using foreach.
Syntax:
foreach (item in itemCollection){ item }
We can use ArrayList foreach like below:
foreach ($demoitem in $demoarrayList){ $demoitem }
It will looks like below:

How to create an ArrayList from an Array in PowerShell?
Now, we will see how to create an arraylist from an array.
Below are two ways we can create an arraylist from an array.
$X=2,4,6,8,9,20,5
[System.Collections.ArrayList]$X
$X=2,4,6,8,9,20,5
$y=[System.Collections.ArrayList]$X
You can see the output like below:

Below is also another way, we can create an arraylist from an array by using the below script:
$X=2,4,6,8,9,20,5
$y= New-Object System.Collections.ArrayList(,$X)
remove item from ArrayList PowerShell
Now, we will see how to remove item from arraylist.
We can use the RemoveAt() method to remove an item from the ArrayList.
$X=2,4,6,8,9,20,5
$y=[System.Collections.ArrayList]$X
$y.RemoveAt(2)

Remove multiple item from arraylist
Now, we will see how to remove multiple items from PowerShell ArrayList by using the RemoveRange() method.
$X=2,4,6,8,9,20,5
$y=[System.Collections.ArrayList]$X
$y.RemoveRange(1,2)
RemoveRange takes two parameter, first parameter is Index and second parameter is Count.
The above code remove 2 items starting from index 1. So it will remove 4,6 from the arraylist.

This is how we can remove multiple items from an arraylist.
You may like following tutorials:
- How to use PowerShell get-date cmdlets
- PowerShell cannot be loaded because running scripts is disabled on this system windows 10
- How to use PowerShell reference variable
- Retrieve all list names and list guids from SharePoint online site using PowerShell
- What is a PowerShell variable
- the term ‘get-spweb’ is not recognized as the name of a cmdlet function
- How to loop through a PowerShell array
In this tutorial we learned what is an arraylist and below things:
- How to Create an ArrayList
- Create empty ArrayList
- Add an item to ArrayList
- Get ArrayList item count
- Sort an ArrayList
- Get all items from ArrayList using foreach
- How to create an ArrayList from an Array
- Remove items from ArrayList
- Remove multiple items from ArrayList
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.