Job
Job

Reputation: 505

How to exclude specific rows in CSV using PowerShell?

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?

Upvotes: 1

Views: 832

Answers (1)

Drew
Drew

Reputation: 4020

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.

Upvotes: 3

Related Questions