user1854377
user1854377

Reputation: 33

Removing element from .NET array .remove method not working as expected (Powershell)

I have some code where I'm trying to use the .NET way of working with arrays in Powershell and the .remove method is not removing the element I want (processname "wssm" in examples 2 and 3). I'm doing something wrong and I'm not sure what it is.

  1. The test example works when I populate specific elements using .add:

    -----------EX1-----------

    $foo = New-object System.Collections.Arraylist
    
    $foo.add("red")
    
    $foo.add("blue")
    
    $foo.remove("red")
    

    $foo returns a single element value "blue" (this is good).

  2. When I try to populate the array with the results of Get-process (using two different methods below) and try to remove the element value "wssm" it does not seem to be able to find and remove it.

    ------------EX2--------------

    $test = New-object System.Collections.Arraylist(,(get-process |select processname))
    
    $test.Remove("wssm")
    

    -------------EX3------------

    $test = New-object System.Collections.Arraylist
    
    $test2= get-process |select processname 
    
    $test.Addrange($Test2)
    
    $test.Remove("wssm")
    

Examples 2 and 3 do not remove the element containing "wssm" and just returns the whole array list of processnames(wssm is shown as being present in the array) and throws no errors.

When I do a

$foo |get-member

It comes back with:

TypeName: System.String

And .remove IS listed as a method. When I do a:

$test |get-member

It comes back with:

TypeName: Selected.System.Diagnostics.Process

And .remove is NOT listed as a method (why this not not throw an error is unknown).

The results of $foo do not contain a header and the results of $test contain a header of "processname" from the select in my get-process step.

Is this a multidimensional array issue and I am just not using the correct syntax in my .remove("wssm") step? Or should I be declaring the array differently?

Thanks for any help.

Upvotes: 2

Views: 3072

Answers (2)

Peter
Peter

Reputation: 306

I saw somebody wrote this code but deleted afterwards:

$test = New-object System.Collections.Arraylist(,(get-process |select -expandProperty processname))

This is good too not sure why it gets deleted.

It seem if we don't expand property the new object "type" will be decided by "get-process" which is " System.Diagnostics.Process".

$test = New-object System.Collections.Arraylist(,(get-process |select  processname))

When we use the "-expandProperty" it becomes system.string:

$test = New-object System.Collections.Arraylist(,(get-process |select -expandProperty processname))

Actually this is a very good example showing up how expandProperty works.

Upvotes: 2

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200373

When you fill the ArrayList with the output of Get-Process you're creating an array of objects. Calling the Remove() method with a string "wssm" won't do anything, because the array doesn't contain such a string object. Instead you need to identify the object with that process name and remove that object from the array:

$wssm = $test | ? { $_.ProcessName -eq "wssm" }
$test.Remove($wssm)

Upvotes: 2

Related Questions