How to create and use PowerShell ArrayList

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.

powershell create arraylist

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
powershell create arraylist of strings

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:

powershell arraylist count

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:

PowerShell arraylist sort

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:

powershell arraylist foreach

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:

powershell create arraylist from array

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 element from PowerShell ArrayList

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.

Remove multiple item from arraylist PowerShell

This is how we can remove multiple items from an arraylist.

You may like following tutorials:

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
  • 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.

  • >