0

I create a PSCustom Object with some Properties and add this to a Collection

in short like

$Collection = @()

foreach ($item in $items)
{
    $obj = New-Object PSCustomObject
    $obj | Add-Member -NotePropertyName Property1 -NotePropertyValue ""
    $obj | Add-Member -NotePropertyName Property2 -NotePropertyValue ""
    $obj | Add-Member -NotePropertyName Property3 -NotePropertyValue ""

$Collection += $obj
}

that was ok so far and worked. Until i wanted to remove something from it. i got a message that the op_substraction is not a Method.

ok so i googled and found out that i could declare the Collection like this

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

i change += to $Collection.Add($obj) now when i did $Collection.Remove($obj) i did not get an Error but the obj was not removed.

i googled more and found [System.Collections.ArrayList] first of all one more info.. i have following code to remove the object ($MyItem contains info which object should not be removed)

foreach ($Item in $Collection)
{
    if ($MyItem -notcontains $Item.Value)
    {
        $Collection.Remove($Item)
    }
}

so if i would do this it gives an error that $Collection was changed. ok so i clone the Opject List. I found some code on SO and changed it a bit

function clone-Collection($obj)
{
    $newobj = New-Object System.Collections.Generic.List[System.Object]
    foreach ($oobj in $obj)
    {
        $nobj = New-Object PsObject
        $oobj.psobject.Properties | % { Add-Member -MemberType NoteProperty -InputObject $nobj -Name $_.Name -Value $_.Value }
        $newobj.Add($nobj)
    }
    
    return $newobj
}

i call the Function and in the function everything is fine. But the ReturnValue now has 0,1,2,... at the beginning. i don't know why. i would like to suppress this.

further i read here that [System.Collections.ArrayList] is depricated.

so i'm pretty much lost. should i even use ArrayList if so how do i get rid of the numbers if i shouldn't use ArrayList what is the right alternativ. or am i doing something basic wrong?

please help me.

Thank you regards

crdy
  • 43
  • 9
  • The `Add()` method outputs the index in the arraylist (0,1,2..). You can hide that using `$null = $newobj.Add($nobj)` or `[void]$newobj.Add($nobj)` – Theo Mar 15 '21 at 12:33

1 Answers1

1

As commented, the Add() method on an ArrayList outputs the index into which the new item is added. To suppress this output, just do $null = $newobj.Add($nobj) or [void]$newobj.Add($nobj)

As for the Remove() method in a Generic List, it works for me if I specify the object to remove correctly:

$Collection = New-Object System.Collections.Generic.List[System.Object]
$items = 'foo', 'bar', 'baz'
foreach ($item in $items) {
    $obj = New-Object PSCustomObject
    $obj | Add-Member -NotePropertyName Property1 -NotePropertyValue $item
    $obj | Add-Member -NotePropertyName Property2 -NotePropertyValue ""
    $obj | Add-Member -NotePropertyName Property3 -NotePropertyValue ""
    $Collection.Add($obj)

    # or simply do
    # $Collection.Add([PsCustomObject]@{Property1 = $item; Property2 = ''; Property3 = ''})
}

To create a copy of the collection, you can do:

$newCollection = [PsCustomObject[]]::new($Collection.Count)
$Collection.CopyTo($newCollection)

Remove one object from the original $Collection:

$obj = $Collection | Where-Object {$_.Property1 -eq 'bar'}
[void]$Collection.Remove($obj)
$Collection

Output:

Property1 Property2 Property3
--------- --------- ---------
foo                          
baz                          
Theo
  • 57,719
  • 8
  • 24
  • 41
  • Thank you. Yes if i specify the object to remove correctly as well it works for me too. i have no clue why i did not see this... i just love Stack Overflow you guys rock – crdy Mar 15 '21 at 13:28