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).
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).
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()
How do I add to this Collection in Powershell? - Stack Overflow
.add and .remove to an array doesn't work in powershell
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-nullHope this helps!
More on reddit.comTwo ways of creating a new System.Collections.ArrayList object, what's the difference?
Could it be because of the new version on powershell that when i'm trying to add a string to an array it doesn't work with the method of .add and .remove?
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
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!
$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
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:
-
for arrB, if I'm reading this right, @() is creating an empty array and then it's being cast into [System.Collections.ArrayList]?
-
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