Utilize a PowerShell Cmdlet to Count Files, Words, and Lines

Dr Scripto

Summary: Larn how to utilise a powerful Windows PowerShell cmdlet to count words and lines in files, or to count files.

Microsoft Scripting Guy Ed Wilson here. The weekend is halfway over in Charlotte, Due north Carolina. For my friends in Australia, the weekend is already over, and they are on their way to piece of work. Of course they become to beginning their weekend earlier than I do. The platonic affair to practise is to be in Australia to start the weekend, and then pop back to Charlotte to conclude the weekend. Yes, I accept strange thoughts on the weekend. For instance, I was on the treadmill before, and I was thinking about my favorite Windows PowerShell cmdlet. Anyhow, I chosen the Scripting Married woman while I was cooling downwards. She was downstairs and it is easier to call her than to become downwardly there. Cell phones brand cracking intercoms.

"What are y'all doing?" she asked equally she answered her Windows 7 phone.

"I just finished running on the treadmill, and I am now cooling down. I was wondering what your favorite Windows PowerShell cmdlet is."

"You have got to be kidding. Why would I accept a favorite Windows PowerShell cmdlet?" she asked.

"Well, I was thinking about my favorite Windows PowerShell cmdlet while I was running, and I realized I did non know what yours was," I said.

"Get-Real," she said as she hung upwardly.

At times, I recall that the Scripting Wife seems to believe I am a nerd. I am not positive of this and am somewhat agape to enquire, but she seems to give off the "nerd alarm" vibe when I enter a room or when I telephone call her on her cell phone from upstairs and enquire her most her favorite Windows PowerShell cmdlet.

Anyway, I will share my favorite cmdlet—it is the Measure-Object cmdlet. If I did not accept the Measure-Object cmdlet, I would demand to count the files in a folder manually. This is shown hither:

$i=0

Get-ChildItem -Path c:\fso -Recurse -Force |

foreach-object { $i++ }

$i

Using the Measure-Object cmdlet, it is piece of cake to count the files. I merely need to use the post-obit steps.

  1. Use the Get-Childitem cmdlet to return a list of fileinfo objects. Apply the recurse switch to cause the cmdlet to work through subfolders. The strength switch is used to return any hidden or organisation files. Pass the path to count to the path parameter.
  2. Pipage the fileinfo objects from step one to the Measure-Object cmdlet

An example of using this command to count the files in the c:\fso folder is shown here:

Go-ChildItem -Recurse -force | Measure-Object

The command and associated output are shown in the following figure. Notation that I ran the command twice: the starting time time without the strength switched parameter, and the second fourth dimension using it.

Image of command and associated output

But the Measure-Object cmdlet does more than simply count the number of files in a folder. It tin also tell me information virtually a text file. A sample file is shown in the following figure.

Image of sample file

If I desire to know how many lines are contained in the file, I use the Measure-Object cmdlet with the line switch. This command is shown here:

Get-Content C:\fso\a.txt | Measure-Object –Line

If I need to know the number of characters, I utilise the graphic symbol switch:

Go-Content C:\fso\a.txt | Measure-Object -Character

At that place is also a words switched parameter that will return the number of words in the text file. Information technology is used similarly to the character or line switched parameter. The command is shown hither:

Get-Content C:\fso\a.txt | Mensurate-Object –Word

In the post-obit figure, I use the Measure-Object cmdlet to count lines; then lines and characters; and finally lines, characters, and words. These commands illustrate combining the switches to return specific information.

Image of using Measure-Object to count

Ane really cool thing I can practise with the Measure out-Object cmdlet is to measure out specific properties of the piped objects. For example, I can utilize the Become-ChildItem cmdlet to return fileinfo objects for all the text files in the folder. I can examine the length belongings and find out the minimum length of the files in the folder, the maximum length, the average size, and the full length of all files in the binder. This command and associated output are shown here:

PS C:\fso> Get-ChildItem -Filter *.txt | Measure-Object -Property length -Maximum -Minimum -Boilerplate -Sum

Count    : 66

Average  : 305903.833333333

Sum      : 20189653

Maximum  : 12534760

Minimum  : 0

Holding : Length

If I desire to, I can pipe the output to a table and create my own custom headings and output. In the following example, I brandish the average size of the files in kilobytes. I also ascertain the format to omit decimal places:

PS C:\fso> Get-ChildItem -Filter *.txt | Measure-Object -Property length -Maximum -Minimum -Average -Sum | ft count, @{"Label"="average size(KB)";"Expression"={($_.average/1KB).tostring(0)}}

                                                      Count                      Average size(KB)

                                                      —–                        —————-

                                                         66                        299

Well, that is most all there to say for at present. The Measure-Object cmdlet is one of my favorite cmdlets considering it is easy to utilize and extremely flexible—an unbeatable combination in my book. What is your favorite cmdlet? Add a annotate below and let me know. Until tomorrow, see ya.

I invite you lot to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. Meet you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy