0

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?

1 Answer 1

3

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.

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

1 Comment

Depending if the file already contains data Add-Content might be considered a better fit. OP made no indication either way but its worth noting. Don't see exporting a file with just a number being useful.

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.