[image] david-zemdegs: Initialise my output array. Most of the times thats unnecessary. [image] david-zemdegs: on each loop initialise an internal array and populate it. Why does it have to be a newly initialized array in each iteration? [image] david-zemdegs: At the end of each โ€ฆ Answer from Olaf on forums.powershell.org
๐ŸŒ
PowerShell Forums
forums.powershell.org โ€บ powershell help
Adding arrays to arraylists - PowerShell Help - PowerShell Forums
July 11, 2022 - I have an object which is of type {system.collections.arraylist] To add a single element I can use the Add method but to add an array I have to use the โ€˜+=โ€™ method. Does anyone have a technical explanation for this as โ€˜+=โ€™ doesnt seem to be in the microsoft doco below: I am also assuming ...
๐ŸŒ
SharePoint Diary
sharepointdiary.com โ€บ sharepoint diary โ€บ powershell โ€บ powershell tutorials โ€บ powershell arraylist โ€“ a beginners guide!
PowerShell ArrayList - A Beginners Guide! - SharePoint Diary
September 30, 2025 - This approach also works for AddRange() and Insert() if you want to keep things clean while adding multiple or specific elements. ... You can access elements just like a standard PowerShell array. ... This iterates through each element sequentially. You can also use a for loop with Count: ... For($i=0; $i -lt $Arraylist.Count; $i++) { # Do something with $element $element = $arraylist[$i] Write-host $Element }
Discussions

powershell - Add an array to an array list - Stack Overflow
OK, I am going to rephrase the question to be both more generic and more complete. But I am leaving the original post at the bottom. Given: a variable $queue, which I want to add different kinds and More on stackoverflow.com
๐ŸŒ stackoverflow.com
arrays - Add new set of values to ArrayList - Stack Overflow
So I have the following ArrayList stored in $var: ip_prefix region string 0.0.0.0/24 GLOBAL Something 0.0.0.0/24 GLOBAL Something 0.0.0.0/24 GLOBAL Something... More on stackoverflow.com
๐ŸŒ stackoverflow.com
powershell - How can you add values into an arraylist - Stack Overflow
I have 2 sets of variables $numbers = "1","2","3" $records = "A","B","C" that I would like to join togther so the final output is Name Value ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
ArrayList.Add Method
Pretty sure you have a typo and you've written $table.Add instead of $tables.Add .. ? More on reddit.com
๐ŸŒ r/PowerShell
15
2
December 8, 2017
๐ŸŒ
SPGuides
spguides.com โ€บ powershell-arraylist
PowerShell ArrayList [Create and Use] โ€“ Learn SharePoint, Microsoft Power Platform and SPFx Tutorials โ€“ SPGuides
December 6, 2024 - In this tutorial, I explained how to create and use an ArrayList in PowerShell with a few examples. I have also shown how to add element, remove elements, access elements or iterate over a PowerShell ArrayList with examples.
๐ŸŒ
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 - When you use +=, PowerShell isnโ€™t just โ€œaddingโ€ a new item to the existing array. Instead, itโ€™s a bit like starting over from scratch every time. PowerShell creates a brand-new, larger array in memory, copies all the old items from the original array into the new one, and then adds the new item. For small arrays, += is fine and easy, but if youโ€™re adding thousands of items, like in a loop processing files, it can get slow because PowerShell rebuilds the array each time. Thatโ€™s where ArrayList shines; itโ€™s built for growth and saves time on big jobs.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 61756527 โ€บ add-an-array-to-an-array-list
powershell - Add an array to an array list - Stack Overflow
The problem with Adamt8's answer is the implicit type coercion. The appended-to $arrList2 in his example becomes an Array, and you end up losing all the advantages of an ArrayList. The answer is <ArrayList>.addRange(<ICollection>).
๐ŸŒ
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
Taking your initial array you can convert it to a System.Collections.ObjectModel.Collection`1 like this, which may be easier to remember than using the full type name of either a collection or array list: $Collection = {$Fruits}.Invoke() $Collection $Collection.GetType() Now we can add and remove items using the Add and Remove methods:
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
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 - Using the addition operator to merging two arrays. Here, we are going to merge the two arrays to the empty array using the โ€˜+โ€™ (plus) operator. ... $PetAnimals = @( "Cow" , "Dog" ) $WildAnimals = @( "Tiger" , "Lion" ) $Animals = @() $Animals = $PetAnimals + $WildAnimals $Animals ... Using Arraylist to add the value.
Call ย  +917738666252
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 12, 2024 - Unlike the += operator, the ArrayList.Add() method appends items to the existing array without creating a new one each time. This approach minimizes memory overhead and enhances script performance, particularly when dealing with large datasets.
๐ŸŒ
PowerShell Test-Path
powershellfaqs.com โ€บ powershell-append-to-array
PowerShell Append to Array [With Examples]
January 27, 2024 - The advantage of using ArrayList is that it is designed for dynamic changes, such as adding or removing items. However, the Add method returns the index of the new item, so if you donโ€™t need this information, you can cast it to [void] to suppress the output: ... You can append a complex expression result to an array in PowerShell using the += operator with a sub-expression $():
๐ŸŒ
Adam the Automator
adamtheautomator.com โ€บ powershell-array
PowerShell Arrays, ArrayLists & Collections: Best Practices
Arrays arenโ€™t just relegated to storing strings as shown above. You can also create arrays with other object types like integers. If you need an array of integers in sequential order, you can take a shortcut and use the range .. operator. Below you can see an array was created with the integers 2 through 5 with a single line of code. PS51> $NumberedArray = 2..5 PS51> $NumberedArray 2 3 4 5 ยท Using a PowerShell ArrayList is also a way in which you can store a list of items with PowerShell.
Published ย  April 22, 2025
๐ŸŒ
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 - The above example will call the New-Object method to instantiate a new ArrayList but this is relatively expensive in terms of computing resources so the above can be rewritten as ... In addition of being shorter it has the added benefit of avoiding the negative performance hit of New-Object and optimizing code is always a good idea especially when dealing with larger scripts. As an alternative you can cast PowerShell Array to ArrayList
๐ŸŒ
Varonis
varonis.com โ€บ blog โ€บ powershell-array
PowerShell Array Guide: How to Use and Create
June 9, 2022 - Here, we can use the default .Net constructor to create a new ArrayList, and then using the -Add operator to add items to it. The [void] operator is there because sometimes these commands throw out strange outputs that can mess with the code.
๐ŸŒ
PowerShell Test-Path
powershellfaqs.com โ€บ add-values-to-an-array-in-powershell
How to Add Values to an Array in PowerShell?
October 3, 2024 - By using either the += operator or the ArrayList class within a ForEach loop, you can efficiently add values to an array in PowerShell.
๐ŸŒ
SPGuides
spguides.com โ€บ powershell-add-item-to-array
How to Add Items to an Array in PowerShell (5 Simple Methods) โ€“ Learn SharePoint, Microsoft Power Platform and SPFx Tutorials โ€“ SPGuides
May 2, 2025 - While the += operator is the simplest way to add items to an array, using ArrayList or List provides significantly better performance for larger collections or frequent modifications. For most of my professional scripting work, I use the generic List for its combination of type safety and ...
Top answer
1 of 2
2

Nice start. Your current approach just needs to account for when you encounter a new key or an existing key.

The main approach you could take is iterate the records, and first check if the record exists as a key in the hash table. If it does, create a new System.Collections.ArrayList, otherwise add the number to the existing key's ArrayList. This assumes that both arrays are equal length.

Here is one way of doing that:

$numbers = "1","2","3"
$records = "A","B","C"

$total = [ordered] @{}
for ($i = 0; $i -lt $records.Length; $i++) {
    $record = $records[$i]

    # Check if record key exists here
    if ($total.Keys -notcontains $record) {

        # Create new array list for the new record key found
        $total[$record] = New-Object -TypeName System.Collections.Arraylist
    }

    # Add item to array list in hashtable
    [void]$total[$record].Add($numbers[$i])
}

$total

For checking if the keys exists, I used -notcontains to check if the key exists in the hashtable Keys. You can have a look at about_comparison_operators for more information on the available operators available in PowerShell.

Additionally, to add an object to an ArrayList, you can use System.Collections.ArrayList.Add(Object). This method returns the type int, which is the index of the value added. This means that you will get the indices outputted to the screen every time an object is added. To suppress this, I casted void, which ignores the return value. If you want to know other ways of doing this, have a look at this Prevent ArrayList.Add() from returning the index question.

Output:

Name                           Value
----                           -----
A                              {1}
B                              {2}
C                              {3}
2 of 2
1

If the braces are un-important, since the value you are displaying is just a hash:

$numbers = "1","2","3"
$records = "A","B","C"
$hash    = [ordered]@{}

for( $i = 0; $i -lt $numbers.Length; $i++ ) {
    for( $j = 0; $j -lt $records.Length; $j++ ) {
        if( $i -eq $j ) {
            $hash[ "$( $records[ $j ])" ] = "$( $numbers[ $i ] )"
            continue
        }
    }
}

$hash

Output:

Name                           Value
----                           -----
A                              1
B                              2
C                              3
๐ŸŒ
Reddit
reddit.com โ€บ r/powershell โ€บ arraylist.add method
r/PowerShell on Reddit: ArrayList.Add Method
December 8, 2017 -

Hey guys

I am trying to add Table Objects into an ArrayList. This always worked but suddenly I am getting an error.

#create table class
class Table
{
    [String] $table_name
    [String] $content_type
    [String] $finishedOn  
}

I add the Table-Objects like this:

 $tables = New-Object System.Collections.ArrayList
 while ($reader.Read())
    {
        $table = New-Object Table
        $table.table_name = $reader["table_name"];
        $table.content_type = $reader["content_type"];
        $table.finishedOn = $LoadDate;
        [void]$tables.Add($table);
    }

The error I'm getting is:

System.Management.Automation.RuntimeException
Method invocation failed because [Table] does not contain a 
method named 'Add'.

I don't really get why PS gives me this error, since I'm using the Add-Method of the ArrayList, and am adding a Table-Object. As far as I know I don't need an add-Method for Table-Objects. Also it's very strange that I never got this error before while trying out my script, but now it's appearing every time.

Do I need to create an Add-Method for my Table-Class? And if so, how would I go about this?

EDIT: Fuck this. When I use Get-Member -InputObject $tables It sometimes tells me $tables is an array, sometimes it tells me it's an arraylist and sometimes it tells me it's a table. It's infuriating (though interesting). I'll try if I can pinpoint where it changes its object-type.

EDIT2: Thanks everyone! I'm really happy there is such a nice and active PowerShell community here on reddit. I hope I can come back to you if I got any other questions and as I'm learning more, hopefully I'll be able to answer some questions of other users! :)

๐ŸŒ
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 - Typically, the most common approach that I see with adding items to an array in PowerShell is through the use of the += operator. ... I had to first initialize the array, otherwise my attempts to add the text would instead be treated as ...