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:

Copy    $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:

Copy$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
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:

Copy    $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:

Copy$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:

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

Adding items is simple.

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

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

Copy$outItems.ToArray()
🌐
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"....
Discussions

.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
How to add a SCCM collection in a folder inside device collection?
I believe what you are looking for is move-cmobject https://learn.microsoft.com/en-us/powershell/module/configurationmanager/move-cmobject?view=sccm-ps You would do something like this $newcol = New-CMDeviceCollection -Name "Test Devices" -LimitingCollectionName "All Systems - Workstations only- OU based" Move-CMobject -inputobject $newcol -folderpath "site:\devicecollection\your\folder" More on reddit.com
🌐 r/PowerShell
4
1
June 4, 2023
PowerShell SCCM add devices to Collection from .txt file with id null or empty
You're not checking to see if the computername in the text file exists in SCCM. something like this should work (not tested, try it out) # Example data $FileOutputData = .\FileOut.txt $ComputerHosts = Get-content $FileOutputData ForEach($Computer in $ComputerHosts) { if ($resourceid = ((get-cmdevice -Name $Computer).Resourceid)) { Write-Host " This is where we add $Computer to SCCM Collection " try { Add-CMDeviceCollectionDirectMembershipRule -CollectionName "Software Automation" -ResourceId $resourceid } catch { Write-Host " Unable to add hostname $omputer to SCCM collection " } } } More on reddit.com
🌐 r/SCCM
8
2
October 23, 2020
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
🌐
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 - Using an ArrayList is an easier and faster than a standard PowerShell array. Here is an example: $names = New-Object System.Collections.ArrayList $names.Add("Paul") $names.Add("John") $names.Add("Simon") $names.Remove("Paul") This method makes it much easier to work with dynamically sized arrays.
🌐
Microsoft Learn
learn.microsoft.com › en-us › powershell › module › microsoft.powershell.core › about › about_arrays
about_Arrays - PowerShell | Microsoft Learn
March 24, 2026 - You can use the ForEach() method to execute an object's method on every item in the collection. ... Just like the ArgumentList parameter of ForEach-Object, the arguments parameter allows the passing of an array of values to a scriptblock configured ...
🌐
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.
🌐
Learn Powershell
learn-powershell.net › 2014 › 09 › 21 › quick-hits-adding-items-to-an-array-and-a-look-at-performance
Quick Hits: Adding Items to an Array and a Look at Performance | Learn Powershell | Achieve More
September 23, 2014 - You won’t find that with the ArrayList approach as it adds the item right into the collection. As with any of these types of performance tests, I covered the simple up the extreme type of situations, so if you absolutely want to squeeze every possible millisecond out of your PowerShell scripts, ...
Find elsewhere
🌐
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.
🌐
Microsoft Learn
learn.microsoft.com › en-us › powershell › module › configurationmanager › add-cmdevicecollectionincludemembershiprule
Add-CMDeviceCollectionIncludeMembershipRule (ConfigurationManager) - Configuration Manager | Microsoft Learn
Add-CMDeviceCollectionIncludeMembershipRule -CollectionName "Device" -IncludeCollectionName "All Systems" This command first uses the Get-CMCollection cmdlet to get the target collection object named Device.
🌐
Microsoft Learn
learn.microsoft.com › en-us › powershell › module › microsoft.powershell.utility › add-member
Add-Member (Microsoft.PowerShell.Utility) - PowerShell | Microsoft Learn
You can use the NotePropertyName and NotePropertyValue parameters to define a note property or use the NotePropertyMembers parameter, which takes a hash table of note property names and values. Also, beginning in Windows PowerShell 3.0, the PassThru parameter, which generates an output object, is needed less frequently. Add-Member now adds the new members directly to the input object of more types.
🌐
Netwrix
blog.netwrix.com › home › resources › how to use powershell arrays
How to Use PowerShell Arrays | Netwrix
December 13, 2024 - Think of it as a collection or a list of items of the same or different data types. Arrays are used in many scripting and programming languages, including Windows PowerShell. Let’s delve into how to create and use an array in PowerShell. By default, each item in an array is an object, rather than another data type like string or integer. Here is an example of how to create an array of objects by explicitly adding ...
🌐
Microsoft Learn
learn.microsoft.com › en-us › powershell › scripting › learn › deep-dives › everything-about-arrays
Everything you wanted to know about arrays - PowerShell | Microsoft Learn
November 17, 2022 - So make sure your arrays are not $null before you try to access elements inside them. Arrays and other collections have a Count property that tells you how many items are in the array. ... PowerShell 3.0 added a Count property to most objects.
🌐
EDUCBA
educba.com › home › data science › data science tutorials › powershell tutorial › powershell add to array
PowerShell add to array | How does PowerShell add to array Works?
March 8, 2023 - PowerShell array is the collection ... array or the array list with the different methods like the plus equal operator (+=) or Add() method and also the multiple arrays with the single or the different data types can be merged into ...
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Delft Stack
delftstack.com › home › howto › powershell › add items to array in powershell
How to Add Items to Array in the PowerShell | Delft Stack
February 24, 2025 - While the += operator is convenient, it’s essential to understand its implications, especially when dealing with large arrays. The += operator creates a new array each time an item is added, copying the existing elements and appending the new one.
🌐
Microsoft Certified Professional Magazine
mcpmag.com › articles › 2019 › 04 › 10 › managing-arrays-in-powershell.aspx
Managing Arrays in PowerShell -- Microsoft Certified Professional Magazine Online
April 10, 2019 - To add an item to an array, we can have PowerShell list all items in the array by just typing out its variable, then including
🌐
Linux Hint
linuxhint.com › array-of-strings-powershell
How to Use an Array of Strings in PowerShell
Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
🌐
O'Reilly
oreilly.com › library › view › windows-powershell-cookbook › 9781449359195 › ch07.html
7. Lists, Arrays, and Hashtables - Windows PowerShell Cookbook, 3rd Edition [Book]
January 10, 2013 - To enable this, PowerShell supports many features to help you through both its language features and utility cmdlets. PowerShell makes working with arrays and lists much like working with other data types: you can easily create an array or list and then add or remove elements from it.
Author   Lee Holmes
Published   2013
Pages   1034
🌐
Microsoft Learn
learn.microsoft.com › en-us › powershell › module › configurationmanager › add-cmdevicecollectiondirectmembershiprule
Add-CMDeviceCollectionDirectMembershipRule (ConfigurationManager) - Configuration Manager | Microsoft Learn
December 1, 2021 - 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.
🌐
Adam the Automator
adamtheautomator.com › powershell-array
PowerShell Arrays, ArrayLists & Collections: Best Practices
This is one scenario in which using an ArrayList may benefit you if you plan to be frequently adding/removing items. ... To retrieve specific items from an array or ArrayList you can use many different methods. Much like other objects in PowerShell, you can access all elements of an array by simply calling the object.
Published   April 22, 2025