LightningWar
LightningWar

Reputation: 975

Remove item from arraylist using a wildcard

I'm trying to remove item(s) from a collection using wildcards. There are multiple elements with similar names:

[System.Collections.ArrayList]$Array = @(
    "server1=localhost"
    "server2=127.0.0.1"
    "server3=12.13.14.15"
    "server4=192.168.1.1"
}

I can remove a single item using the remove method: $Array.Remove('server1=localhost')

Is there a way to remove an item with the wildcard character *? For example:

 $Array.Remove('server*')

But this does not work.

Upvotes: 1

Views: 1438

Answers (2)

G42
G42

Reputation: 10019

You could do this:

($Array | Where-Object {$_ -like "Server*"}) | ForEach-Object {$Array.Remove($_)}

Upvotes: 0

Martin Brandl
Martin Brandl

Reputation: 58961

There is no Remove method that takes a wildcard but you can do that using the Where-Object cmdlet:

$Array = $Array | Where-Object {$_ -NotLike "server1*"}

Upvotes: 1

Related Questions