How to Count the Number of Characters, Words and Lines in PowerShell

Rohan Timalsina Feb 02, 2024
  1. Use Measure-Object Cmdlet to Count the Number of Characters in PowerShell
  2. Use Measure-Object Cmdlet to Count the Number of Words in PowerShell
  3. Use Measure-Object Cmdlet to Count the Number of Lines in PowerShell
  4. Use Measure-Object Cmdlet to Count Text of All Files of the Directory in PowerShell
  5. Use measure to Count the Number of Characters, Words and Lines in PowerShell
How to Count the Number of Characters, Words and Lines in PowerShell

While working with text files, you may need to know the number of characters, words, and lines in a file. You can find many tools and websites to count characters, words, and lines.

But if you want to use the command line, PowerShell is a great tool. The Measure-Object cmdlet calculates the property values of objects in PowerShell.

It displays the numeric values for characters, words, and lines in string objects, such as text files. This tutorial teaches how to count the number of characters, words and lines in a text file using PowerShell.

Use Measure-Object Cmdlet to Count the Number of Characters in PowerShell

The Measure-Object cmdlet does calculations depending on the parameters used in the command. For instance, it measures the property values, such as characters, words, and lines of string objects, only if the parameters are specified on the command.

The -Character parameter tells Measure-Object to calculate the number of characters in the input objects.

The following command counts the number of characters in a test.txt file in the directory C:\New.

Get-Content C:\New\test.txt | Measure-Object -Character

Output:

Lines Words Characters Property
----- ----- ---------- --------
                  1065

In the above example, the Get-Content cmdlet gets the content of a test.txt file. It is then piped to Measure-Object as an input object to calculate its number of characters.

By default, the white spaces are counted as characters. You can exclude the white spaces in a character count by using the -IgnoreWhiteSpace parameter.

Get-Content C:\New\test.txt | Measure-Object -Character -IgnoreWhiteSpace

Output:

Lines Words Characters Property
----- ----- ---------- --------
                   894

The other values are not displayed because their parameters are not specified.

Use Measure-Object Cmdlet to Count the Number of Words in PowerShell

The -Word parameter instructs Measure-Object to calculate the number of words in the input objects.

The example below counts the number of words in a test.txt file in the directory C:\New.

Get-Content C:\New\test.txt | Measure-Object -Word

Output:

Lines Words Characters Property
----- ----- ---------- --------
        173

Use Measure-Object Cmdlet to Count the Number of Lines in PowerShell

The -Line parameter instructs Measure-Object to calculate the number of lines in the input objects.

The example below counts the number of lines in a test.txt file in the directory C:\New.

Get-Content C:\New\test.txt | Measure-Object -Line

Output:

Lines Words Characters Property
----- ----- ---------- --------
    7

You can use all parameters in the single command to count the total number of characters, words and lines in a file.

Get-Content C:\New\test.txt | Measure-Object -Character -Word -Line

Output:

Lines Words Characters Property
----- ----- ---------- --------
    7   173       1065

Use Measure-Object Cmdlet to Count Text of All Files of the Directory in PowerShell

You can also count the number of characters, words and lines of all files present inside the directory. The following command measures the characters, words, and lines of all .txt files in the C:\New directory.

Get-ChildItem C:\New\*.txt | Get-Content | Measure-Object -Character -Word -Line

The Get-ChildItem gets all .txt files in the C:\New directory in the above command. Its output is piped to the Get-Content to get the content of all files, which is then piped to Measure-Object to calculate the number of characters, words, and lines.

Output:

Lines Words Characters Property
----- ----- ---------- --------
  303  2379      36046

If you want to count the number of files present in sub-directories, too, you have to use the -Recurse parameter with Get-ChildItem.

The following example displays the number of characters, words, and lines of all .txt files in the directory C:\New, including its sub-directories.

Get-ChildItem C:\New\*.txt -Recurse | Get-Content | Measure-Object -Character -Word -Line

Output:

Lines Words Characters Property
----- ----- ---------- --------
 3488 21646     191389

Use measure to Count the Number of Characters, Words and Lines in PowerShell

You can also measure text in text files using the measure command, the built-in alias for Measure-Object.

This command will print the number of characters, words and lines of a process.txt file in the C:\New directory.

Get-Content C:\New\process.txt | measure -Character -Word -Line

Output:

Lines Words Characters Property
----- ----- ---------- --------
  293  2189      34867

In this article, you have learned to use Measure-Object to count the number of characters, words, and lines of text files in PowerShell.

Rohan Timalsina avatar Rohan Timalsina avatar

Rohan is a learner, problem solver, and web developer. He loves to write and share his understanding.

LinkedIn Website