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?
The first one is to get the content of the files ( Get-Content ) and count the lines in there. With the Select-String cmdlet, you can count your code lines in a file or script using PowerShell. You can find more about Select-String on Microsoft Docs.
The backtick character (`) represents an escape character in Powershell. An escape character lets us print otherwise unprintable characters to the standard output. The purpose of escape characters is to represent whitespace characters like: \t (tab), \n (newline), and \r (carriage return).
On output, PowerShell uses the platform-native newline sequence: `r`n on Windows, `n on Unix-like platforms. This applies to use of text-file-creating cmdlets, which comprise: Cmdlets for plain-text file creation: Set-Content and Out-File / redirection operator > .
Get-ChildItem -Filter "*.cs" -Recurse | Get-Content | Measure-Object -line
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With