Count Number of Lines, Characters, Words In Files Using PowerShell

0 Comments
Editor Ratings:
User Ratings:
[Total: 0   Average: 0/5]




This tutorial explains how to count the total number of lines, words, and characters in files using PowerShell. I have covered this tutorial to count these stats for one file, or for all the files in a folder, and also total for all the files. You can do this for all files, or files of a specific type. Although many word counter software, tools, and websites exist, but when you don’t want to use third-party tools, then perhaps PowerShell can be a handy option to use.

Using PowerShell, you will be able to count the total number of words, lines, and/or characters for a single file, all files available in a folder, and all files including their file names in a particular directory. I have added all the commands in this tutorial.

How To Count The Total Number of Words, Lines, and Characters For A File Using PowerShell?

This option is useful when you want to count words, characters, and lines present in a specific file (say TXT, PDF, etc.).

Step 1: Open PowerShell using the Search box. If you want to open PowerShell for a particular folder, then you can just open File Explorer, access that folder, and type “powershell” in the address bar of File Explorer, and press enter to open PowerShell for that particular folder. Or else, you can also open PowerShell for a folder using the context menu.


Step 2: After opening PowerShell for a folder, you need to type the command to find a total number of words, characters, and lines for a particular file. The command would be: Get-Content "source location of file along with file extension" | measure -Line -Character -Word

In the command above, add file path and location. If you opened PowerShell in the folder in which file is present, you can just give the filename.

find number of lines, words, characters available in a file using powershell

Execute the command and result will be in front of you, like you can see in the above screenshot. So, you can see how easy it is to find the number of words, characters, and lines in a file using PowerShell.

How To Count The Total Number of Lines, Words, and Characters for All Files of A Folder In A Single Line Using PowerShell?

This option is helpful when you want to find the total number of lines, characters, and words for all files available in a specific folder. You won’t be able to see file names separately or total number of words, characters, and lines for individual files. Only the total Stats are generated using this option.

Step 1: Access PowerShell for a particular folder as mentioned above.

Step 2: Type “Get-ChildItem -Recurse | Get-Content | Measure-Object -Word -Line -Character” and press enter.

The result will show the total number of lines, words, and characters present in all those files in a single line.

The above command helps to find word and character Stats for all the files of a folder. However, when you have to check these stats for a particular file type (say TXT), then you need to add *.file extension* in the above command. So, let’s say you want to check word and character Stats for all the TXT files present in a folder, then your command would be:

Get-ChildItem -Filter *.txt -Recurse | Get-Content | Measure-Object -Word -Line -Character.

In a similar way, you can change the file extension in the above command to check Stats for some other file type.

How To Count The Total Number of Lines, Words, and Characters Available In All Files Along With File Names Present In A Folder Using PowerShell?

This is the most powerful option and really helpful when you want to find total words, lines, and characters for all files including their file names available in a folder using PowerShell.

Step 1: Like the above two options, you need to open PowerShell for a specific folder.

Step 2: Now paste the following code and press enter:

dir -Include *.* -Recurse |
% { $_ | select name, @{n="characters";e={
get-content $_ |
measure-object -character |
select -expa characters }
} , @{n="words";e={
get-content $_ |
measure-object -word |
select -expa words }
} , @{n="lines";e={
get-content $_ |
measure-object -line |
select -expa lines }
}
} | ft -AutoSize

This will show the list of files with their file names and total number of characters, lines, and words available under each individual file.

In the code above, I have highlighted a part in Red. This part is used to control which type of files should be included. For example, if you want to include only text files, change it to *.txt , and so on.

The Verdict:

So, these are some really beneficial commands to count total number of words, lines, and characters for a single or multiple files using PowerShell. When you don’t want to take the help of some 3rd party tool for word count, then perhaps, PowerShell is one of the best option available to choose from.

Editor Ratings:
User Ratings:
[Total: 0   Average: 0/5]