user1807815
user1807815

Reputation: 93

Using PowerShell to get a count from a csv file and export to csv

I have a csv file with name,id,age. I have imported it properly and am looking for a count of all names that are null(empty). I have this so far:

@(Import-Csv C:\Mine\Desktop\f1.txt | Where-Object {$_.name -eq ""}).Count

This prints out the correct number of empty names into the prompt, but if I export it, nothing shows up in the txt file.

@(Import-Csv C:\Mine\Desktop\f1.txt | Where-Object {$_.name -eq ""}).Count | Export-Csv C:\Mine\Desktop\tests.txt -notype

So it shows the correct number in the command prompt, but when I try to export it, it creates the .txt but it is empty. Any help would be appreciated. Also, if I wanted to know how many null entries their are in the entire CSV (from name,id,age). Would the best way be to do this line Where-Object {$_.name -eq ""} but change $_.name every time? Or is there a shorter version?

Upvotes: 0

Views: 1928

Answers (1)

TheMadTechnician
TheMadTechnician

Reputation: 36342

The problem is that you are taking an integer (the count of entries with blank names) and trying to export a CSV. Export-CSV expects you to have an object, or array of objects with properties that it can convert to a table in CSV format, with property names as the header row, and values below that for each object.

What you want is to just output that value to a file, so try replacing Export-CSV with Set-Content (and remove the -notype) to set the content of the file you specify to the number delivered by your command.

Upvotes: 3

Related Questions