Reputation: 1522
I have a CSV with several rows and two columns: "Name" and "Information".
I'm looping through the CSV and checking each line for a condition, if the condition is met I'd like to remove the line:
for ($i=0; $i -le $CSV.length; $i++)
{
if ($CSV.name == "Fred")
{
#remove $CSV[$i] -- that one line; both "Name" and "Information"
}
}
I've seen solutions that use Get-Content
or Import-Csv
and temporary file(s) but I haven't wrapped my head around it and figure there must be an easier way.
Regardless, any help is appreciated!
Upvotes: 2
Views: 1943
Reputation: 58931
I would read the CSV using the Import-CSV
cmdlet and use the Where-Object
cmdlet to filter the entries. Finally, write the CSV back using the Export-CSV
cmdlet:
$csv = import-csv 'YourPath.csv'
$csv | Where Name -ne 'Fred' | Export-Csv 'YourPath.csv' -NoTypeInformation
Upvotes: 4