user10210238
user10210238

Reputation:

How would I use an array with the contains operator

I have a CSV file and in that file are 2 columns (this is just for testing purposes before using actual files), I need to get all the data for the specific pets so Dog and Cat in this example.

I have put the animals I want the data for in an array but for some reason the statement is not working it just returns an empty file, when I use a string (so just "dog") in the place of the array then I get the data back fine. Is there anything i'm doing wrong

$Pets = "Dog", "Cat"

import-csv -Path "C:\Users\User\deletingRows.csv" | where Animal -Contains $Pets | export-csv -Path "C:\Users\User\Result.csv" -NoTypeInformation

Upvotes: 0

Views: 44

Answers (1)

Floogen
Floogen

Reputation: 306

I believe you're misunderstanding what -Contains is supposed to do. Since you're trying to match a field against more than one value (within $Pets), you're better of using the -In switch. See the working example below.

$Pets = "Dog", "Cat"

Import-Csv -Path "C:\Users\User\deletingRows.csv" | where "Animal" -In $Pets | Export-Csv -Path "C:\Users\User\Result.csv" -NoTypeInformation

Upvotes: 1

Related Questions