Why cant I store a hashtable in an arraylist?

Viewed 1246

This is my code

$allTests = New-Object System.Collections.ArrayList
$singleTest = @{}

$singleTest.add("Type", "Human")

1..10 | foreach {

    $singleTest.add("Count", $_)
    $singleTest.add("Name", "FooBar...whatever..$_")
    $singleTest.add("Age", $_)
    $allTests.Add($singleTest) | out-null

    $singleTest.remove("Count")
    $singleTest.remove("Name")
    $singleTest.remove("Age")
}

From my understanding my loop should be adding a copy of the hashtable to the arraylist everytime it gets to

$allTests.Add($singleTest) | out-null

the loop continues on, removes some keys and this paves the way for the next iteration of the loop . Thats not what happening, its like the add command is only adding a reference to the hashtable.

If I check the final value of

$allTests

this gets returned

Name                           Value                                                                                                                   
----                           -----                                                                                                                   
Type                           Human                                                                                                                   
Type                           Human                                                                                                                   
Type                           Human                                                                                                                   
Type                           Human                                                                                                                   
Type                           Human                                                                                                                   
Type                           Human                                                                                                                   
Type                           Human                                                                                                                   
Type                           Human                                                                                                                   
Type                           Human                                                                                                                   
Type                           Human     

How do I fix this so a actual copy of the hashtable is stored in the array list ?

I'm looking for an ouput like

$allTests[0]

Name                           Value                                                                                                                   
----                           -----                                                                                                                   
Count                          1                                                                                                                       
Name                           FooBar...whatever..1                                                                                                    
Age                            1                                                                                                                       
Type                           Human  

$allTests[1]   

Name                           Value                                                                                                                   
----                           -----                                                                                                                   
Count                          2                                                                                                                       
Name                           FooBar...whatever..2                                                                                                    
Age                            2                                                                                                                       
Type                           Human  
3 Answers
Related