Count the number of characters, words and lines in PowerShell Count the number of characters, words and lines in PowerShell powershell powershell

Count the number of characters, words and lines in PowerShell


Get-Content [FILENAME] | Measure-Object -Character

It counts the number of characters in the file.

Get-Content [FILENAME] | Measure-Object -Word

It counts the number of words in the file.

Get-Content [FILENAME] | Measure-Object -Line

It counts the number of lines in the file.


Measure-Object do exactly that.You do have to specify the parameters you want measured for the characters, words and lines to be returned.

Example 3: Measure text in a text file

This command displays the number of characters, words, and lines inthe Text.txt file. Without the Raw parameter, Get-Content outputs thefile as an array of lines.

The first command uses Set-Content to add some default text to a file.

"One", "Two", "Three", "Four" | Set-Content -Path C:\Temp\tmp.txtGet-Content C:\Temp\tmp.txt | Measure-Object -Character -Line -WordLines Words Characters Property----- ----- ---------- --------    4     4         15

Reference: Microsoft.Powershell.Utility/measure-object