1

I have a csv file that looks like this:

NO   ID     TOTAL
1    123    1000
2    456    1500
3    123    2500
4    112    4000

I want to exclude the rows where ID=123 and the TOTAL>3000.

$File = Import-Csv 'SELL.csv'
   $File | Where-Object { $_.ID -eq "123", $_.TOTAL -lt "3000" } -Exclude |
   Export-Csv 'SELLREPORT.csv' -NoType

What am I doing wrong?

1 Answer 1

3

You have the syntax all wrong. Try this one.

$File = Import-Csv 'SELL.csv'
$File | Where-Object { 
    ($_.ID -ne '123') -or ([int]$_.TOTAL -lt 3000)
} | Export-Csv 'SELLREPORT.csv' -NoType

Adding [int] will ensure your cells are read as integers and not strings. This will allow us to use -ne, Not Equal, and -ge, Greater than or equal to.

This won't exclude matched results, but include results that don't match.

Sign up to request clarification or add additional context in comments.

6 Comments

how if I want to make it exclude matched result? @Drew
@Job This will do what you are after, there is no -exclude option for Where-Object.
It still not work for me. If I have csv data like this NO ID TOTAL 0 123 1000 1 456 1500 2 123 2500 3 112 4000 4 123 4000 I can only output the file like this NO ID TOTAL 3 112 4000 I got it, I have to use or not and And the other problem is, how if my $ID is not integer, I change the $ID to "A", "B".
I am having difficulties determining your requirements based on your English. Are you expecting it to exclude results if EITHER of the conditions are met, but not both?
I have updated the answer to suit your updated requirements
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.