top of page
  • Writer's pictureUzair Ansari

Add and remove elements from Powershell Array

Updated: Nov 5, 2021

Ever wondered how to add or remove elements from array of powershell or ever faced the error 'Exception calling "Remove" with "1" argument(s): "Collection was of a fixed size."' while adding or removing the elements of array in powershell? Here's the article which will be of help.

By default an array object in powershell has fixed size. Hence adding or removing an element from array requires some additional logic to be applied. First, we will see how to add or remove an array element in powershell using the default collection type.

The first command will create an array (which is of fixed size) of car names and store it in $Cars variable.

Second command will check the object type of the $Cars variable.


As we can see, the object type is showing as array.

Let's try to add or remove some elements from the array.


As we can see the attempt to add or remove elements from the array failed with the error 'Exception calling "Remove" with "1" argument(s): "Collection was of a fixed size."

That's because the array which we defined was of fixed sized and cannot be modified.

To solve this, we can use ArrayList class from System.Collections namespace and create an ArrayList which will allow us to add or remove the array elements.

We can do this by casting the variables to the ArrayList class using the step mentioned below.


As we can see we are using the same steps we used earlier except that we have added [System.Collections.ArrayList] class. This tells the shell to store the variables in the form of ArrayList.


We will now add and remove variables from the $Cars variable.












The GMC element gets added to the array list. We will now try to remove some element from the array.











We saw that the "Audi" element was successfully removed from the array.

bottom of page