How to add/remove values in the array in PowerShell?

Not Found
Not Found

An array is always a fixed size. To add value to the array, you need to create a new copy of the array and add value to it. To do so, you simply need to use += operator.

For example, you have an existing array as given below.

$array = 1,2,3,4,5

To add value “Hello” to the array, we will use += sign.

$array += "Hello"

Now, we will check the output of the array.

We have another method to add value to the array. By Add() operation of the array.


When you use the above method to add a variable in the given array, you will get below error.

Exception calling "Add" with "1" argument(s): "Collection was of a fixed size."
At line:1 char:1
+ $array.Add("Hi")
+ ~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : NotSupportedException

This is because the array size is fixed. You can check if the array is a fixed size or not by using the below method.


When you check the type of this array, it is an object, not the list.

To deal with the above problem, we need to use System.Collection.ArrayList instead.

When you check the type of this array, it will be an array list.

So from the array list, we can add or remove items.



Updated on 14-Feb-2020 06:47:06