How to RemoveAll when using generic list in powershell

Question:

If I use generic list like this:

How can I apply RemoveAll() method of List<T>? Is it possible to use predicates? How can I for example remove all items that start with capital ‘F’?

Answer:

I think the only way is without using System.Predicate that needs delegates (sorry, but really I can’t figure out how create anonymous delegates in powershell) and use the where-object clause.

In my example I re-assign the result to same $foo variable that need to be cast again to list<T>.

To avoid error if result count is only one it need the ‘,’ to create always an array value

EDIT:

After some test I’ve found how use lambda expression using powershell scriptblock:

This is the right way for using method that needs System.Predicate in powershell

Source:

How to RemoveAll when using generic list in powershell by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply