Any quick way to check if any row in CSV file contains a specific value?

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 7 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
grathal
Posts: 98
Last visit: Tue Nov 27, 2018 4:16 am

Any quick way to check if any row in CSV file contains a specific value?

Post by grathal »

Hi there,

I have a CSV file called somefile.csv like the following:

ServerName,DoSomeThing
Tom,FALSE
Dick,FALSE
Harry,TRUE

In a script, I want to load the file and then stop the script if $DoSomeThing is FALSE everywhere.

Do I have to use a where loop on each line, like below, or is there something more succint like -Contains that will work here?

$x = Import-Csv somefile.csv
if ($x | where {$_.DoSomeThing -eq $True}) { write-verbose "something to do"}

Thanks,

- Alan.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Any quick way to check if any row in CSV file contains a specific value?

Post by jvierra »

$x | where {$_.DoSomeThing -eq 'True'}
User avatar
grathal
Posts: 98
Last visit: Tue Nov 27, 2018 4:16 am

Re: Any quick way to check if any row in CSV file contains a specific value?

Post by grathal »

Ok thanks I'll use that version.

Using $True does seem to work though?
This topic is 7 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked