Reputation: 787
I'm trying to -match
more than one string in a CSV file. Sample CSV below:
Hostname,IpAddress,Domain
x200apple300,1.2.3.4,contoso.local
x200banana300,1.2.3.5,contoso.local
x200orange300,1.2.3.6,contoso.local
x200kiwi300,1.2.3.7,contoso.local
Sample code:
$csv = Import-Csv "C:\Files\csvExample.csv"
$csv | Where-Object {$_.Hostname -match "kiwi"}
# All OK. Now match multiple hostnames.
$csv | Where-Object {$_.Hostname -match "kiwi", "orange"}
# Not working.
$csv | Where-Object {$_.Hostname -contains "apple", "banana"}
# Not working
How can I match multiple hostnames in this example? What am I doing wrong.
Upvotes: 0
Views: 753
Reputation: 32145
The -match
operator expects a regex on the right hand side. I'm not sure what $Value -match $A, $B
means; it's not documented that I can see and it seems to always return false from my input.
$_.Hostname -contains "apple", "banana"
is backwards. You want either {$_.Hostname -in "apple", "banana"}
or {"apple", "banana" -contains $_.Hostname}
.
Upvotes: 3
Reputation: 1200
Match takes a regex expression, so you can use an or
operator to filter on multiple values.
$csv = Import-Csv "C:\Files\csvExample.csv"
$csv | Where-Object {$_.Hostname -match "kiwi|orange|banana"}
Upvotes: 3