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()
.add and .remove to an array doesn't work in powershell
How to add a SCCM collection in a folder inside device collection?
PowerShell SCCM add devices to Collection from .txt file with id null or empty
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.comVideos
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?