Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell - Removing Items from Array

I'm trying to remove items from an array that match special keywords. My array looks something like this:

$Printers =@('Printer Phone', 'Printer Building1', 'Printer XML', 'Printer Station', ...) 

I want to remove all entries that match parts of certain strings, like filtering out every item that has "Phone" or "XML" in it's value. I thought of something like this but I'm not quiet sure if I'm on the right track:

$contains = @('Phone', 'XML')
$Printers -Filter * | Where-Object { $contains -contains $_.name }

Is there a simple way to achieve this?

like image 963
pyte Avatar asked Mar 09 '26 10:03

pyte


1 Answers

Here is one way to do this.

> $contains = @('Phone', 'XML')
> $Printers = @('Printer Phone','Printer Building1','Printer XML', 'Printer Station')
> $Printers | Where-Object { $_ -Match ($contains -Join "|") }
Printer Phone
Printer XML
> $Printers | Where-Object { $_ -notMatch ($contains -Join "|") }
Printer Building1
Printer Station
like image 166
Abdul Niyas P M Avatar answered Mar 12 '26 02:03

Abdul Niyas P M