Skip to content

Instantly share code, notes, and snippets.

@moiaune
Last active May 20, 2021 13:34
Show Gist options
  • Save moiaune/3dbb024814e65605d09d82ca2a4af113 to your computer and use it in GitHub Desktop.
Save moiaune/3dbb024814e65605d09d82ca2a4af113 to your computer and use it in GitHub Desktop.
Get-LinesOfCode
# Get lines of code in a powershell project
# Inspiration: https://www.limilabs.com/blog/source-lines-of-code-count-using-powershell
function Get-LinesOfCode {
[CmdletBinding()]
param (
[Parameter()]
[string]
$Path = "."
)
$Path = Resolve-Path -Path $Path
$linesOfCode = Get-ChildItem -Path $Path -Filter *.ps1 -Recurse |
Select-String "^\s*$" -NotMatch | # Filter out blank lines
Select-String "^\s*#" -NotMatch | # Filter out lines starting with comments
Select-String "^\s*<#" -NotMatch | # Filter out lines starting with '<#' (multiline-line comments)
Select-String "^\s*\.\w*" -NotMatch # Filter out lines starting with . and then som characters (e.g .PARAMETER)
Write-Output $linesOfCode.Count
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment