For a quick count in PowerShell, run Get-Content .file.txt | Measure-Object -Line. To report source lines by language and separate blank, comment, and source lines across a project, use cloc . from the project directory.
Count lines in one text file with PowerShell
Open PowerShell, change to the folder containing the file, and run:
Get-Content .file.txt | Measure-Object -Line
Replace file.txt with the file’s name or path. The result includes a Lines value. Microsoft’s Measure-Object documentation shows this method for counting lines in strings.
By default, Get-Content returns newline-delimited strings. Its -Raw option instead returns the file contents as a single string, changing the input shape. Use the plain command above for the documented simple line-count workflow.
Do these 3 things before closing this tab:
1Fix the driver behind crashes, sound loss and screen glitches2Clear out junk files and repair common Windows errors3Scan for outdated or missing drivers - takes under a minute#1 Best Overall
Count selected source files in a project
This example counts text lines from C# and PowerShell files beneath the current directory:
Get-ChildItem -Path . -Recurse -File -Include *.cs,*.ps1 |
ForEach-Object { Get-Content -LiteralPath $_.FullName } |
Measure-Object -Line
Change *.cs and *.ps1 to match your project. The extensions determine what is included, so decide whether hidden directories, generated output, build folders, or vendored dependencies belong in the count. The pipeline counts lines of text in the selected files; it does not identify comments, blank lines, or executable code.
Rank #2
Get a language-aware report with cloc
For a source-code report, use cloc. From the project directory, run:
cloc .
The report distinguishes blank lines, comment lines, and physical lines of source code across supported languages. The project documents Windows support: you can run its released Windows executable without Perl, or use its Perl-source version with a Perl interpreter such as Strawberry Perl. Follow the repository’s current instructions to obtain it; filenames such as version 2.10 in the README are examples, not a guarantee of the latest release.
Rank #3
To make a result reproducible, note the cloc version, the directory or repository revision counted, the language/file scope, and any exclusions. A working directory may contain uncommitted or generated files, so it may not match a particular committed revision.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Choose and describe the count you need
“Lines of code” can mean different things. A physical count measures source-text lines under a tool’s rules; a logical count attempts to measure executable statements and relies more on language-specific rules. Reformatting the same program can change its physical line count without changing its behavior.
Rank #4
| Method | Best for | What it reports | Setup |
|---|---|---|---|
PowerShell Get-Content and Measure-Object -Line |
A text file or a deliberately selected set of files | Line total for the text read; no code/comment/blank classification | Built into PowerShell |
| cloc | A language-aware report of a project tree, repository, or files | Blank, comment, and physical source-line counts for supported languages | Windows executable, or Perl source route with Perl installed |
When comparing totals, use the same method and file scope. State whether the number is physical or logical and what files or revision it covers; a line total by itself does not establish code quality or development effort.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.
Recommended Free Tools

