Menu & Search
Count rows within CSV files using PowerShell

Count rows within CSV files using PowerShell

This post is a quick run-through of using a PowerShell script to count rows within a bunch of CSV files. You might find this useful for verifying row counts, if importing multiple CSV files into a database.

First, open PowerShell ISE on your machine.

Have a quick look at your directory.

Now, I’ll count the rows for each file using the script below.

This can be run against network paths by entering ‘\\server-name\share\’ instead of ‘C:\Temp’ as I’m doing.

We can copy that information out for later, paste into Excel and run a quick Text to Columns choosing comma delimiter.

! – I need a script that’ll export this straight into the CSV file; initial attempt failed.

We can see above that one of the files has a count of -1. This indicates that the file has no data, so we can throw it in the bin.

Next up, I’m going to check for a common issue I’ve seen happen. Exported CSV files containing “(12345 rows affected)” at the end of it.

Now let’s have a look at that file…

Looks like someone (me) has exported data from SQL Server without adding SET NOCOUNT ON. We’ll need to remove that as highlighted, and update the row counts for that file to 34.

This is an area I hope to expand on in future, but for now, that’s it.