MSFTNEXT

How to Get Number of Words, Chars and Lines in File in Windows 10

Today we will see how to get number of words, chars and lines in a file in Windows 10. Sometimes it is useful to learn this info about a text file. When you are in Windows 10 without extra apps installed, PowerShell can be used for that.

PowerShell is a cross-platform task automation and configuration management framework, consisting of a command-line shell and scripting language. Unlike most shells, which accept and return text, PowerShell is built on top of the .NET Common Language Runtime (CLR), and accepts and returns .NET objects. This fundamental change brings entirely new tools and methods for automation. So, it is an advanced command prompt. It includes plenty of ready-to-use cmdlets. Also, it allows using .NET framework for extending them. For our task, we need one of such cmdlets.

The Measure-Object cmdlet

PowerShell includes the cmdlet Measure-Object. It can calculate the property values of certain types of object. Measure-Object performs three types of measurements, depending on the parameters in the command. The cmdlet can count objects and calculate the minimum, maximum, sum, and average of the numeric values. For text objects, it can count and calculate the number of lines, words, and characters.

You need to send the file contents as a cmdlet parameter. There are a number of ways you can do it, but you better combine Measure-Object with the another cmdlet Get-Content  which simple prints all file lines to the console.

Get Number of Words, Chars and Lines in File

  1. Open PowerShell
  2. Type or paste the following command: Get-Content "c:\my folder\my file.txt" | Measure-Object -Line -Character -Word. 
  3. Change the file path to the file you want to measure.
  4. Use the following command to count the same excluding spaces: Get-Content "c:\my folder\my file.txt" | Measure-Object -Line -Character -Word -IgnoreWhiteSpace. 

That’s how it can be done.

In the screenshots above we were measuring the built-in license file available in Windows 10. PowerShell is really useful, as it included in Windows 10 out of the box.

Looks like you've read the post to the end. If you enjoyed it, please share it. You would greatly help our blog to grow!
Exit mobile version