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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With