Powershell Script to remove rows from a CSV

Hi all.
I would like a script to remove all lines\rows from a .CSV log where an entry of "0" is found in the 3rd column. The csv does not have headers.
Does anyone know how I can do this using powershell?

Thanks very much.
jamiegfAsked:
Who is Participating?
 
oBdAConnect With a Mentor Commented:
You need to add as many "ColX" to the Header argument of Import-Csv as the file has columns; the scripts below assume 3.
If you want/need the fields enclosed in double quotes:
Import-Csv -Path C:\Temp\input.csv -Header Col1, Col2, Col3 |
	Where-Object {$_.Col3 -ne '0'} |
	ConvertTo-Csv -NoTypeInformation |
	Select-Object -Skip 1 |
	Set-Content -Path C:\Temp\output.csv

Open in new window

Without quotes:
Import-Csv -Path C:\Temp\input.csv -Header Col1, Col2, Col3 |
	Where-Object {$_.Col3 -ne '0'} |
	ConvertTo-Csv -NoTypeInformation |
	Select-Object -Skip 1 |
	ForEach-Object {$_.Replace('"', '')} |
	Set-Content -Path C:\Temp\output.csv

Open in new window

0
 
jamiegfAuthor Commented:
You absolute legend! thanks very much.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.