36

How can I count the number of rows in a csv file using powershell? I tried something like

Get-Content -length "C:\Directory\file.csv"

or

(Get-Content).length "C:\Directory\file.csv"

but these result an error.

6 Answers 6

66

Get-Content and Measure-Object are fine for small files, but both are super inefficient with memory. I had real problems with large files.

When counting rows in a 1GB file using either method, Powershell gobbled up all available memory on the server (8GB), then started paging to disk. I left it over an hour, but it was still paging to disk so I killed it.

The best method I found for large files is to use IO.StreamReader to load the file from disk and count each row using a variable. This keeps memory usage down to a very reasonable 25MB and is much, much quicker, taking around 30 seconds to count rows in a 1GB file or a couple of minutes for a 6GB file. It never eats up unreasonable amounts of RAM, no matter how large your file is:

[int]$LinesInFile = 0
$reader = New-Object IO.StreamReader 'c:\filename.csv'
 while($reader.ReadLine() -ne $null){ $LinesInFile++ }

The above snippet can be inserted wherever you would use get-content or measure-object, simply refer to the $LinesInFile variable to get the row count of the file.

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

8 Comments

Faster than any other solutions shown here. Less than 5 seconds to sort out a 500mb csv file.
You also should dispose reader ($reader.Dispose()) after use or you risk holding file opened until you close your PS session.
If using powershell add "write-output $LinesInFile" at end of block above to get the value on screen.
just add $LinesInFile at the end and you will see the number.
@Linga Search for powershell scripts that process lists of files in a loop, insert my snippet inside the loop and specify the file name as a variable inside your loop.
|
46

Pipe it to the Measure-Object cmdlet

Import-Csv C:\Directory\file.csv | Measure-Object

3 Comments

Thanks, this seems to work, but it is terribly slow compared e.g. to GNU Unix utils wc.exe.
That is because wc.exe would be the equivalent of (Get-Content).Length which while it is much faster than Import-CSV, it is also a potentially incorrect solution as pointed out by stej since it would not account for rows with multiline fields.
(took ~1 minute for a 100 MB file)
12

Generally (csv or not)

@(Get-Content c:\file.csv).Length

If the file has only one line, then, it will fail. (You need the @ prefix...otherwise if the file has one line, it will only count the number of characters in that line.

Get-Content c:\file.csv | Measure-Object -line

But both will fail if any record takes more than one row. Then better import csv and measure:

Import-Csv c:\file.csv | Measure-Object | Select-Object -expand count

6 Comments

Last one seems to generate an error: Select-Object : Cannot expand property "count" because it has nothing to expand. At line:1 char:64 + Import-Csv C:\Directory\file.csv | Measure-Object | Select-Object <<<< -expand count
Weird, Measure-Object should return an object that has a property Count. Try to remove the |Select-Object ... and you will see what it returns.
You're probably using PowerShell v1. In v1, 'Select-Object -expand propertyName' throws an error when the result is a scalar (one object). Upgrade to v2 and you're good to go.
Thanks, Yes, I'm using version 1.0, it's one of the tags of this question.
I check only PowerShell tag. And silently assumed that nobody uses v1.0. Sorry :)
|
3

You can simply use unix like comand in powershell.

If you file test.csv Then command to get rowcount is

gc test.csv | Measure-Object

Comments

0

You can try

(Import-Csv C:\Directory\file.csv).count

or

$a=Import-Csv C:\Directory\file.csv
$a.count

1 Comment

I get an OutOfMemoryException using this approach on a large file. Using Get-Content | Measure-Object works with a limited amount of memory..
-1

(Import-Csv C:\Directory\file.csv).count is the only accurate one out of these.

I tried all of the other suggestions on a csv with 4781 rows, and all but this one returned 4803.

Comments

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.