Find the Amount of Words, Chars and Lines in a Text File Using PowerShell

If you’re still using the good, old Notepad to craft essays or content for your Website, and want to know the number of characters used in a text file, here is a PowerShell cmdlet that will help.

PowerShell has a built-in cmdlet named “Measure-Object”. It calculates 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. This is exactly what we’re going to make use of, to get the summary of text based files.

Get the Line, Character and Word Count in a Text File

Start PowerShell, type or paste the following command and press ENTER.

Get-Content "D:\Net-Helpmsg.txt" | Measure-Object -Line -Character -Word

To ignore whitespace when counting the number of characters and words, use additional switch as below:

Get-Content "D:\Net-Helpmsg.txt" | Measure-Object -Line -Character -Word -IgnoreWhiteSpace

get word count in text file

In this example, the text file Net-Helpmsg.txt contains 24 lines, 333 words and 1839 characters.

Text editors and Word processors

Modern text editors such as Notepad++ and others show you the line count, number of characters and words used, in the status bar as you type. In Notepad++ you can view the stats by clicking Summary… from the View menu. The Summary dialog also shows the byte count, which isn’t shown in the Status bar.

get word count in text file

In Microsoft Word, click on the word count metric shown in the status bar.

get word count in text file

This opens the Word Count dialog that shows the file summary that includes number of pages, words, characters (with and without spaces), paragraphs and lines in that file.

get word count in text file

I’m sure vast majority of users would prefer third-party text editors or word processors with rich text formatting capabilities and other advanced features. However, users who still use the plain and simple Notepad to write articles, can find this post a tad useful.

About the author

Ramesh Srinivasan founded back in 2005. He is passionate about Microsoft technologies and has a vast experience in the ITeS industry — delivering support for Microsoft's consumer products. He has been a Microsoft MVP [2003 to 2012] who contributes to various Windows support forums.

Leave a Comment