To count the data rows in every CSV file in a folder, run this, changing the path to yours:
Get-ChildItem 'C:\myfolder' -Filter *.csv -Recurse | ForEach-Object {
[pscustomobject]@{
File = $_.Name
Rows = @(Import-Csv $_.FullName).Count
}
}Import-Csv reads the first line as the header, so the count is data rows only. The @() keeps a header-only file at 0. Output from a test folder:
File Rows ---- ---- blanks.csv 2 customers.csv 3 empty.csv 0 export.csv 3 notes.csv 2
Counting rows is a quick way to check an import: count each file, load them, and compare with the row counts of the tables in SQL Server. To keep the counts, add | Export-Csv C:\temp\csv-row-counts.csv -NoTypeInformation to the end. -NoTypeInformation is needed in Windows PowerShell 5.1; PowerShell 7 does it by default and warns the switch is obsolete.
Line Count or Record Count
The original version of this script counted lines and took one off for the header:
Get-ChildItem 'C:\myfolder' -Filter *.csv -Recurse | ForEach-Object {
$lines = (Get-Content $_.FullName | Measure-Object -Line).Lines
"$($_.Name): $($lines - 1) rows"
}Open any PowerShell window to run it. The screenshots here are from 2018, in the PowerShell ISE:


The 2018 version printed the full path and the count separated by a comma, so the output in the screenshot looks a little different from the code above. The -1 is a completely empty file: no header, so nothing to take one off. A comma-separated list pastes straight into Excel, where Text to Columns splits it:

A line count and a record count are not always the same thing. The two methods on the same test files, run in both Windows PowerShell 5.1 and PowerShell 7 with identical results:
| File | What is in it | Import-Csv | Lines minus 1 |
|---|---|---|---|
| customers.csv | Header and 3 rows | 3 | 3 |
| notes.csv | 2 rows, one with a line break inside a quoted field | 2 | 3 |
| blanks.csv | 2 rows and 2 blank lines | 2 | 2 |
| export.csv | 2 rows, a blank line and “(2 rows affected)” | 3 | 3 |
| empty.csv | Header only | 0 | 0 |
| zero.csv | 0 bytes | 0 | -1 |
Measure-Object -Line skips blank lines, which is why the blank lines do not show in either count. A line break inside a quoted field is the case where the line count is wrong and Import-Csv is right. Neither catches a footer such as “rows affected”: that needs the check further down.
Large Files
Import-Csv builds an object for every row, which gets slow and memory hungry on files with millions of rows. For a quick count on big files, stream the lines with .NET instead:
Get-ChildItem 'C:\myfolder' -Filter *.csv -Recurse | ForEach-Object {
$lines = 0
foreach ($line in [System.IO.File]::ReadLines($_.FullName)) { $lines++ }
[pscustomobject]@{ File = $_.Name; Rows = $lines - 1 }
}This counts every line, blank ones included, and never holds the whole file in memory. Pass $_.FullName rather than a relative path: .NET resolves relative paths against the process folder, not your current PowerShell location.
Checking for Data Issues
A common problem when exporting from SQL Server: files end with a “rows affected” line, which breaks the import. It comes from running the query with Results to Text in SSMS without SET NOCOUNT ON. To find the files that have it:
# find CSV files containing "rows affected"
Get-ChildItem 'C:\myfolder' -Filter *.csv -Recurse |
Select-String -Pattern 'rows affected' -List |
Select-Object Path, LineNumber, Line

Blank lines can also trip an import. This lists each blank line by file and line number:
# find blank lines in CSV files
Get-ChildItem 'C:\myfolder' -Filter *.csv -Recurse |
Select-String -Pattern '^\s*$' |
Select-Object Filename, LineNumberFilename LineNumber -------- ---------- blanks.csv 3 blanks.csv 5 export.csv 4
If the counts still will not match what landed in the table, paste a few lines of the file into Claude Code and ask what would trip the import. That’s what it’s there for.
Frequently Asked Questions
Does Import-Csv count the header row?
No. It uses the first line as the column names, so .Count is data rows only.
Why does my count not match the table after an import?
The usual causes are blank lines, a “rows affected” footer, line breaks inside quoted fields, or the import treating the header as data. The checks above find the first three.
How do I count rows in a single CSV file?
@(Import-Csv C:\myfolder\file.csv).Count.
What is the fastest way to count lines in a very large file?
Stream it with [System.IO.File]::ReadLines() as in the Large Files section, rather than loading it with Import-Csv or Get-Content.
Related
If SQL Server is part of your day job, my current work lives over at sqldba.blog: production DBA scripts, an error library and the SSMS guide.
Leave a Reply