When using the $array.Add()-method, you're trying to add the element into the existing array. An array is a collection of fixed size, so you will receive an error because it can't be extended.

$array += $element creates a new array with the same elements as old one + the new item, and this new larger array replaces the old one in the $array-variable

You can use the += operator to add an element to an array. When you use it, Windows PowerShell actually creates a new array with the values of the original array and the added value. For example, to add an element with a value of 200 to the array in the $a variable, type:

    $a += 200

Source: about_Arrays

+= is an expensive operation, so when you need to add many items you should try to add them in as few operations as possible, ex:

$arr = 1..3    #Array
$arr += (4..5) #Combine with another array in a single write-operation

$arr.Count
5

If that's not possible, consider using a more efficient collection like List or ArrayList (see the other answer).

Answer from Frode F. on Stack Overflow
🌐
Vexx32
vexx32.github.io › 2020 › 02 › 15 › Building-Arrays-Collections
Building Arrays and Collections in PowerShell
Let's take a look at some fairly ... sound, at least at first glance. So… spot anything amiss? The pattern here is "create the collection object, and then add each item to it one at a time"....
Top answer
1 of 3
352

When using the $array.Add()-method, you're trying to add the element into the existing array. An array is a collection of fixed size, so you will receive an error because it can't be extended.

$array += $element creates a new array with the same elements as old one + the new item, and this new larger array replaces the old one in the $array-variable

You can use the += operator to add an element to an array. When you use it, Windows PowerShell actually creates a new array with the values of the original array and the added value. For example, to add an element with a value of 200 to the array in the $a variable, type:

    $a += 200

Source: about_Arrays

+= is an expensive operation, so when you need to add many items you should try to add them in as few operations as possible, ex:

$arr = 1..3    #Array
$arr += (4..5) #Combine with another array in a single write-operation

$arr.Count
5

If that's not possible, consider using a more efficient collection like List or ArrayList (see the other answer).

2 of 3
185

If you want a dynamically sized array, then you should make a list. Not only will you get the .Add() functionality, but as @frode-f explains, dynamic arrays are more memory efficient and a better practice anyway.

And it's so easy to use.

Instead of your array declaration, try this:

$outItems = New-Object System.Collections.Generic.List[System.Object]

Adding items is simple.

$outItems.Add(1)
$outItems.Add("hi")

And if you really want an array when you're done, there's a function for that too.

$outItems.ToArray()
Discussions

How do I add to this Collection in Powershell? - Stack Overflow
I'm pulling some data down in a REST API from our ITSM solution, it gets parsed from json and put into an object called $listOfCurrentUsers. I'm then creating an object from data that I pull from A... More on stackoverflow.com
🌐 stackoverflow.com
.add and .remove to an array doesn't work in powershell
Arrays are fixed-size collections -- you can't .Add() and .Remove() from them, and never have been able to. Use a [System.Collections.Generic.List[string]] instead: $List = [System.Collections.Generic.List[string]]::new() $List.Add("hello") $List.Add("world") $List $null = $List.Remove("world") $List More on reddit.com
🌐 r/PowerShell
20
8
February 7, 2025
Adding items to a custom object?

It is important to note that what you are doing is creating a new object, $stuff, with 2 properties: data1 and data2.

What you are showing in your example output is actually what you would be getting from a collection of objects. So, first we will create a collection object. For simplicity's sake, we will just make a standard empty array object to hold the objects in the collection:

$collection = @()

Now, we will add the items one at a time into the collection:

$collection += new-object psobject -property @{data1="foo1";data2="bar1"}
$collection += new-object psobject -property @{data1="foo2";data2="bar2"}
$collection += new-object psobject -property @{data1="foo3";data2="foo3"}

Showing the contents of collection will give us:

$collection
data2           data1
-----            -----
bar1            foo1
bar2            foo2
bar3            foo3

There are ways to make this faster, for instance create the collection as an arraylist and use the .add() method to add objects into the collection: [edit for clarity: By 'faster', I mean in instances where you are working with a large amount of data, not necessarily faster to create the script. For most uses that I'm aware of / have tried, the standard empty array @() will work just fine.]

$collection = new-object system.collections.arraylist
$collection.add(new-object psobject -property @{data1="foo";data2="bar"})

The only downside to using an arraylist is that the .add() method returns the current index that the item is being added at, so adding 5 objects into the collection would show the following in the console:

0
1
2
3
4

To circumvent this, pipe the $collection.add() method to out-null, so that it eats the output:

$collection.add(new-object pscustomobject -property @{data1="foo";data2="bar"}) | out-null

Hope this helps!

More on reddit.com
🌐 r/PowerShell
4
2
February 18, 2016
Two ways of creating a new System.Collections.ArrayList object, what's the difference?
Yes, you create an empty array then use it to initialize the variable. Two other ways (System not needed as its namespace is already loaded): [Collections.ArrayList]$list = @() $list = [Collections.ArrayList]::new() New-Object is useful when you need -ComObject, or to dynamically create a type (typename stored as a string that you may not know when writing). It is the 'older' method before powershell had classes built-in. Also, if you create an 'untyped' variable from a cast, you can change its type later on. The first example I posted enforces the type for that variable, it can only be an arraylist. I recommend using the generic types where you can specify the type: [Collections.Generic.List[String]]$list = @() $list = [Collections.Generic.List[Int]]::new() Also save some typing and use a namespace: using namespace System.Collections.Generic [List[String]]$list = @() $list = [List[Int]]::new() And for speed (enumeration access), if the items are unique, try a HashSet: using namespace System.Collections.Generic [HashSet[String]]$list = @() $list = [HashSet[Int]]::new() You should be measuring speed on insertion/removal and access for real world speed tests. And you'll definitely see different results when you go past milestones like 10, 1000, 10000 items (generics are tailored to large datasets). More on reddit.com
🌐 r/PowerShell
11
27
April 7, 2023
🌐
Andreasbijl
andreasbijl.com › powershell-create-collections-of-custom-objects
PowerShell – Create collections of custom objects | AndreasBijl.com
November 25, 2013 - Sadly I had to disappoint him, it wasn’t on my blog, yet. However I knew the answer and I will now also share this on my blog. Lets start of with creating an ArrayList in PowerShell: $collectionVariable = New-Object System.Collections.ArrayList · Done. This is our generic collection, it can contain any PowerShell (.NET) object. Before adding items to the collection be very aware that the fields of the first item added dictate which fields the collection will have.
🌐
Medium
medium.com › @oakhelloworld › powershell-collection-968226024f38
Powershell: Collection. PowerShell provides a variety of data… | by Oak HelloWorld | Medium
September 28, 2023 - 6. ArrayLists: `System.Collections.ArrayList` is similar to a generic list but allows you to store objects of various types in the same list. $myArrayList = New-Object System.Collections.ArrayList $myArrayList.Add(“String”) $myArrayList.Add(123) 7. Dictionaries (Generic): PowerShell 7 introduced generic dictionaries, which are strongly typed and offer better performance when compared to hash tables.
🌐
Tachytelic
tachytelic.net › home › add items to a powershell array
Add items to a Powershell Array
September 4, 2019 - If you still require a standard PowerShell array you can do: ... Hope this helps. ... $array = [System.Collections.ArrayList]@() #Fill with a list of devices from a function earlier in script $array = get_alldevices #emtpy array $empty = [System.Collections.ArrayList]@() ... Typicall I would have done this like: $array | Foreach-Object { $empty += get_detailsfunction $_ } For large arrays this is VERY slow and not efficient, so I was hoping to use .Add to fill the empty array faster.
🌐
Stack Overflow
stackoverflow.com › questions › 57102462 › how-do-i-add-to-this-collection-in-powershell
How do I add to this Collection in Powershell? - Stack Overflow
I'm then creating an object from data that I pull from Active Directory. It then checks to see if the AD user exists in the $listOfCurrentUsers and if it does, it adds the record ID to the $CAServiceDesk object. If the user doesn't exist, it still adds the record ID, but gives it a value of null.
🌐
Jonathanmedd
jonathanmedd.net › 2014 › 01 › adding-and-removing-items-from-a-powershell-array.html
Adding and Removing Items from a PowerShell Array - Jonathan Medd's Blog
\[System.Collections.ArrayList\]$ArrayList = $Fruits $ArrayList.GetType() Now if we try the previous add and remove methods we are successful: $ArrayList.Add("Kiwi") $ArrayList $ArrayList.Remove("Apple") $ArrayList · Alternatively, if we had stuck with the original Array object we could do the following to ‘add’ an item to it.
🌐
Practical 365
practical365.com › home › powershell › practical powershell: crafting collections
Practical PowerShell: Crafting Collections | Practical365
June 3, 2025 - It does require PowerShell v7, e.g. $dictionary.Add(‘key’, ‘value’) Finally, I have stored all the code that is mentioned throughout the series in a GitHub repository for your convenience. You can find it here. Tags: .NET Framework, PowerShell, PowerShell Collections · Michel de Rooij, with over 25 years of mixed consulting and automation experience with Exchange and related technologies, is a consultant for Rapid Circle. He assists organizations in their journey to and using Microsoft 365, primarily focusing on Exchange and associated technologies and automating processes using PowerShell or Graph.
Find elsewhere
🌐
SharePoint Diary
sharepointdiary.com › sharepoint diary › powershell › how to add items to an array in powershell?
How to Add Items to an Array in PowerShell? - SharePoint Diary
September 20, 2025 - As explained above, you can combine two arrays into a single collection using the + operator and appending them. In addition to the + operator, PowerShell has a comma operator (,) that you can use to create a new array.
🌐
Red Gate Software
red-gate.com › home › powershell one-liners: collections, hashtables, arrays and strings
PowerShell One-Liners: Collections, Hashtables, Arrays and Strings | Simple Talk
January 23, 2019 - Entries here also show how to obtain just the unique elements in a collection as well as adding to collections. Once you have a collection chances are you might want to re-order it per the needs of your application. You can do this with derived properties almost as easily as with simple named properties. The last few entries show how to apply sorting to file contents as well. If you are used to relying on LINQ-to-Object operators in C# so much that you may are almost compelled to reject PowerShell out of hand, fear not!
🌐
System Center Dudes
systemcenterdudes.com › home › automatically create 155 operational sccm collections using this powershell script
Automatically Create 155 Operational SCCM Collections using this Powershell Script - System Center Dudes
November 12, 2024 - All you need to do is run the SCCM collection PowerShell script on your SCCM server and wait. In about 5 minutes, you’ll end up having 155 collections in an Operational folder. The SCCM collections are set to refresh on a 7-day schedule.
🌐
Prajwal Desai
prajwaldesai.com › home › add multiple devices to sccm collection using powershell
Add Multiple Devices to SCCM collection using PowerShell » Prajwal Desai
March 2, 2020 - At line:1 char:146 + … alo Alto 10 percent” -ResourceID (Get-CMDevice -Name $_).ResourceID } + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [Add-CMDeviceCol…tMembershipRule], ParameterBindingValidationExceptio n + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.ConfigurationManagement.PowerSh ell.Cmdlets.Collections.AddDeviceCollectionDirectMembershipRule ... Cause the objects are not exist in your directory. ... Your articles are always useful and easy to understand. Thank you for sharing. ... If servers/systems need to be added or removed from the list, is it as simple as updating the .txt file, or will it also require executing the powershell command again as well?
🌐
Reddit
reddit.com › r/powershell › adding items to a custom object?
r/PowerShell on Reddit: Adding items to a custom object?
February 18, 2016 -

I know not a lot about custom objects. I want to do something easy. At this point I'm just making a hash table, adding content to it, then making a new custom object that reflects the data in that hash table. I want to be able to keep adding data to this custom object moving forward. Something like this...

$things=@{}
$things.data1="foo"
$things.data2="bar"

$stuff=new-object psobject -property $things

$stuff
data2            data1
-----              -----
bar                foo                                                                                       

OK so far. Now I want to add more. How do I get to this?

$stuff
data2            data1
-----              -----                                                                                     
bar                foo
morebar        fooALLTHETIME 
Top answer
1 of 3
5

It is important to note that what you are doing is creating a new object, $stuff, with 2 properties: data1 and data2.

What you are showing in your example output is actually what you would be getting from a collection of objects. So, first we will create a collection object. For simplicity's sake, we will just make a standard empty array object to hold the objects in the collection:

$collection = @()

Now, we will add the items one at a time into the collection:

$collection += new-object psobject -property @{data1="foo1";data2="bar1"}
$collection += new-object psobject -property @{data1="foo2";data2="bar2"}
$collection += new-object psobject -property @{data1="foo3";data2="foo3"}

Showing the contents of collection will give us:

$collection
data2           data1
-----            -----
bar1            foo1
bar2            foo2
bar3            foo3

There are ways to make this faster, for instance create the collection as an arraylist and use the .add() method to add objects into the collection: [edit for clarity: By 'faster', I mean in instances where you are working with a large amount of data, not necessarily faster to create the script. For most uses that I'm aware of / have tried, the standard empty array @() will work just fine.]

$collection = new-object system.collections.arraylist
$collection.add(new-object psobject -property @{data1="foo";data2="bar"})

The only downside to using an arraylist is that the .add() method returns the current index that the item is being added at, so adding 5 objects into the collection would show the following in the console:

0
1
2
3
4

To circumvent this, pipe the $collection.add() method to out-null, so that it eats the output:

$collection.add(new-object pscustomobject -property @{data1="foo";data2="bar"}) | out-null

Hope this helps!

2 of 3
4
$stuff = @()

$stuff += New-Object psobject -prop @{
    data1 = 'foo'
    data2 = 'bar'
}

$stuff += New-Object psobject -prop @{
    data1 = 'morebar'
    data2 = 'fooALLTHETHINGS'
}

# and if you want to add a new property, these both achieve the same result
$stuff | % {$_ | Add-Member -MemberType NoteProperty -Name data3 -Value 'new'}
$stuff = $stuff | % {$_ | select *, @{n='data4';e={'somethingelse'}}}

# but then when you want to add another object should specify all the properties

$stuff += New-Object psobject -prop @{
    data1 = 'test1'
    data2 = 'test2'
    data3 = 'test3'
    data4 = 'test4'
}

$stuff

*edited

🌐
Reddit
reddit.com › r/powershell › two ways of creating a new system.collections.arraylist object, what's the difference?
r/PowerShell on Reddit: Two ways of creating a new System.Collections.ArrayList object, what's the difference?
April 7, 2023 -

I've come across two ways of creating a new System.Collections.ArrayList object:

$arrA = New-Object System.Collections.ArrayList
$arrB = [System.Collections.ArrayList]@() 

I have two questions:

  1. for arrB, if I'm reading this right, @() is creating an empty array and then it's being cast into [System.Collections.ArrayList]?

  2. Working with the created object is the same for me either way, but are there any differences I may be missing?

I made a quick test and casting was faster, but the difference is only noticeable when creating new arrays reaches the hundreds of thousands to millions (tested on an old A10-7870k). Doesn't really matter when I'm creating one array, but I thought it was mildly interesting.

$max = 100000
$ticksStart = (Get-Date).Ticks
for ($i = 0; $i -lt $max; $i++)
{
	$arrA = New-Object System.Collections.ArrayList
}
$ticksEnd = (Get-Date).Ticks
Write-Host 'arrA (ticks)	: ' ($ticksEnd - $ticksStart)

$ticksStart = (Get-Date).Ticks
for ($i = 0; $i -lt $max; $i++)
{
	$arrB = [System.Collections.ArrayList]@()
}
$ticksEnd = (Get-Date).Ticks
Write-Host  'arrB (ticks)	: ' ($ticksEnd - $ticksStart)
arrA (ticks)	:  64310000
arrB (ticks)	:  3170000
🌐
TheSleepyAdmins
thesleepyadmins.com › 2018 › 10 › 03 › adding-multiple-devices-to-sccm-collection-powershell
Adding Multiple Devices To SCCM Collection PowerShell – TheSleepyAdmins
October 28, 2018 - $collectionname = “RDS Deploy Collection” $Computers = Get-content “c:\temp\Server_List.txt” $logpath = “c:\temp” foreach($computer in $computers) { try { Write-Host “Adding $computer to $collectionname” -ForegroundColor Green Add-CMDeviceCollectionDirectMembershipRule -CollectionName $collectionname -ResourceId (get-cmdevice -Name $computer).ResourceID } catch { Write-Warning “Cannot add client $computer object may not exist” $computer | Out-File “$logpath\$collectionname-invalid.log” -Append $Error[0].Exception | Out-File “$logpath\$collectionname-invalid.log” -Append } } There is also a log file that will be export to give devices that have not been added and the error exception generated by PowerShell.
🌐
Microsoft Learn
learn.microsoft.com › en-us › powershell › module › configurationmanager › add-cmdevicecollectiondirectmembershiprule
Add-CMDeviceCollectionDirectMembershipRule (ConfigurationManager) - Configuration Manager | Microsoft Learn
Get-CMCollection -Name "testCollection" | Add-CMDeviceCollectionDirectMembershipRule -ResourceId 16777219 · Specify the ID of the device collection to add the rule. This value is the CollectionID property, for example, XYZ00012.
🌐
PsCustom Object
pscustomobject.github.io › powershell › Add-Remove-Items-From-Array
PowerShell add or remove elements from an Array - PsCustom Object - Hitchikers GUID(e) to Automation
December 24, 2018 - PowerShell will overwrite existing array with the new content · All of this is transparent to the user so you won’t see any difference. If you want to avoid all the copying/moving data you can instantiate myArray as a ArrayList which is dynamic and will allow you to add remove elements on the fly · # Initialize object $myArrayList = New-Object System.Collections.ArrayList($null) # Add elements to list [void]($myArrayList.Add(1)) [void]($myArrayList.Add(2)) # Print array length 2
🌐
Timmyit
timmyit.com › 2016 › 07 › 14 › sccm-and-powershell-adding-nodes-to-a-collection-and-trigger-evaluation
SCCM and Powershell! adding nodes to a collection and trigger evaluation – TimmyIT.com
August 10, 2016 - As for $computers you can stick with that as long as you create that .txt file and inputs the computers you want to be added in the device collection or maybe you want to get them directly from AD you can do that for example.