Find the number of lines in a project with powershell

Viewed 27339

I'm trying to find a way to count the total number of lines in all of the source files of a project I have. I've tried piping dir -r -name into measure-object -line, but that just counts the number of files I have.

Does anyone have a script to do this?

8 Answers
Get-ChildItem -Filter "*.cs" -Recurse | Get-Content | Measure-Object -line

Thanks to everyone who answered. The way I ended up implementing this was

dir . -filter "*.cs" -Recurse -name | foreach{(GC $_).Count} | measure-object -sum

GC is alias for Get-Content
dir is alias for Get-ChildItem

Get-ChildItem . -Include *.txt -Recurse | foreach {(Get-Content $_).Count}

Condensed down a bit with aliases:

GCI . -Include *.txt -Recurse | foreach{(GC $_).Count}

Will give results similar to this:

Lines Words               Characters              Property
----- -----               ----------              --------
   21
   40
   29
   15
  294
   13
   13
  107

EDIT: Modified to recurse through subfolders.

EDIT 2: Removed use of Measure-Object.

dir **.txt -recurse | select Fullname,@{name="LineCount";expression={ @(get-content $_.fullname).count }}

Since I needed something similar this is what I came up with.

one file type: GET-ChildItem -Recurse -Filter '*.cs' | Get-Content | Measure-Object -line

multiple file types: GET-ChildItem -Recurse -Include '.cs','.aspx', '*.ascx' | Get-Content | Measure-Object -line

Count No. of lines in a file inside a directory:

GCI . -Include *.* -Recurse | foreach{(GC $_).Count}

Count SUM of lines in a file inside a directory:

GCI . -Include *.* -Recurse | foreach{(GC $_).Count} | measure-object -sum
Related