37

I don't have much PowerShell experience yet and am trying to teach myself as I go along.

I'm trying to make some proof of concept code for a bigger project. The main goal here is too dynamically create and add elements to an array using a function.

Here is my code:

$testArray = @()
function addToArray($Item1)
{
    $testArray += $Item1
    "###"
}

$tempArray = "123", "321", "453"
$foldertest = "testFolder"

foreach($item in $tempArray)
{
    addToArray $item
}
"###"

Every time the function finishes the array becomes empty. Bear in mind most of my programming experience comes from Java, PHP, some C and C++ just to name a few, if I did this in PHP (adjusting the language syntax of course) this would have worked fine.

2
  • 1
    Why not use a list instead? Arrays were never meant to be operated dynamically. Every time you resize an array, it creates a new one under the hood. So you get the overhead proportional to its size. Commented Nov 10, 2012 at 19:23
  • thanks for the advice neolisk, i did think of that, i'll give it a shot. Commented Dec 18, 2012 at 21:49

5 Answers 5

51
$testArray = [System.Collections.ArrayList]@()
$tempArray = "123", "321", "453"

foreach($item in $tempArray)
{
    $arrayID = $testArray.Add($item)
}
Sign up to request clarification or add additional context in comments.

Comments

21

The problem is one of scope; inside your addToArray function change the line to this:

$script:testArray += $Item1

...to store into the array variable you are expecting.

2 Comments

Note that the drawback of this method is that it creates an in-memory copy of the entire existing array with the new item in it. In other words, it's O( 2n ) memory usage. If you're doing this a lot or with very large arrays, it becomes very expensive. Using an ArrayList reuses the same object and adds the item to it directly.
Quite so, @BaconBits; thanks for adding that important caveat when dealing with larger arrays.
7

NOTE: If you want to have dynamic (non-fixed) amount of items, a more precise solution might be using the List:

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

$tempArray = "123", "321", "453"

foreach($item in $tempArray)
{
    $testArray.Add($item)
}

Note: In this case you get the power of the List from .Net, so you can easily apply linq, merge, split, and do anything which you'd do with the List in .Net

Comments

3

Instead of re-creating the array in each loop iteration (which is basically what's happening each time you add to it), assign the result of the the loop to a variable:

$testArray = foreach($item in $tempArray)
{
    addToArray $item
}

1 Comment

thanks for the advice i may try that next time i go to make a complex script
0

If you want to add an item at the beginning of an array (as index 0), this may work, like array_unshift($array, $item) on PHP:

$tempArray = "123", "321", "453"
$foldertest = "testFolder"
$tempArray = @($foldertest) + $tempArray

This gives:

PS D:\users\user1\temp> $temparray
testFolder
123
321
453

1 Comment

It's a PowerShell question, not PHP. PHP arrays are significantly different to PowerShell arrays since PowerShell arrays are a fixed length. To "add" to a PowerShell array, under the hood it basically creates a new array with the new length and copies every element to it. I believe in PHP they are dynamic (more like a PowerShell Generic list). You may want to consider removing your answer to avoid confusing readers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.