Source lines of code count using PowerShell

Source lines of code (SLOC) is a software metric used to measure the size of a software program by counting the number of lines in the text of the program’s source code.

As we all know the disadvantages of this metric, sometimes we simply want to know.

Here’s a PowerShell script, that recursively searches for *.cs files and counts the lines (empty lines and comments are excluded)

(dir -include *.cs -recurse | select-string "^(s*)//" -notMatch | select-string "^(s*)$" -notMatch).Count

Brief description of what all parts are doing:

  • dir -include *.cs -recurse : Lists all *.cs files, you can add additional extensions using a comma.
  • select-string “^(s*)//” -notMatch : Exclude comments.
  • select-string “^(s*)$” -notMatch : Exclude empty lines.

Tags:

2 Responses to “Source lines of code count using PowerShell”

  1. Gary Woodfine Says:

    There are a few syntactical errors in the code supplied therefore it doesn’t appear to work. Thought I would just let you know.

  2. Limilabs support Says:

    @Gary

    What kind of errors? This code works – we’ve used it many times.

Leave a Reply