Posts Tagged ‘PowerShell’

Source lines of code count using PowerShell

Monday, March 29th, 2010

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.