简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM