{"id":12451,"date":"2011-10-09T00:01:00","date_gmt":"2011-10-09T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/10\/09\/use-a-powershell-cmdlet-to-count-files-words-and-lines\/"},"modified":"2011-10-09T00:01:00","modified_gmt":"2011-10-09T00:01:00","slug":"use-a-powershell-cmdlet-to-count-files-words-and-lines","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-a-powershell-cmdlet-to-count-files-words-and-lines\/","title":{"rendered":"Use a PowerShell Cmdlet to Count Files, Words, and Lines"},"content":{"rendered":"<p><strong>Summary:<\/strong> Learn how to use a powerful Windows PowerShell cmdlet to count words and lines in files, or to count files.<\/p>\n<p>&nbsp;<\/p>\n<p>Microsoft Scripting Guy Ed Wilson here. The weekend is halfway over in Charlotte, North Carolina. For my friends in Australia, the weekend is already over, and they are on their way to work. Of course they get to start their weekend earlier than I do. The ideal thing to do is to be in Australia to start the weekend, and then pop back to Charlotte to conclude the weekend. Yes, I have strange thoughts on the weekend. For example, I was on the treadmill earlier, and I was thinking about my favorite Windows PowerShell cmdlet. Anyway, I called the Scripting Wife while I was cooling down. She was downstairs and it is easier to call her than to go down there. Cell phones make great intercoms.<\/p>\n<p>&ldquo;What are you doing?&rdquo; she asked as she answered her Windows 7 phone.<\/p>\n<p>&ldquo;I just finished running on the treadmill, and I am now cooling down. I was wondering what your favorite Windows PowerShell cmdlet is.&rdquo;<\/p>\n<p>&ldquo;You have got to be kidding. Why would I have a favorite Windows PowerShell cmdlet?&rdquo; she asked.<\/p>\n<p>&ldquo;Well, I was thinking about my favorite Windows PowerShell cmdlet while I was running, and I realized I did not know what yours was,&rdquo; I said.<\/p>\n<p>&ldquo;Get-Real,&rdquo; she said as she hung up.<\/p>\n<p>At times, I think that the Scripting Wife seems to believe I am a nerd. I am not positive of this and am somewhat afraid to ask, but she seems to give off the &ldquo;nerd alert&rdquo; vibe when I enter a room or when I call her on her cell phone from upstairs and ask her about her favorite Windows PowerShell cmdlet.<\/p>\n<p>Anyway, I will share my favorite cmdlet&mdash;it is the <b>Measure-Object<\/b> cmdlet. If I did not have the <b>Measure-Object<\/b> cmdlet, I would need to count the files in a folder manually. This is shown here:<\/p>\n<p style=\"padding-left: 30px\">$i=0<\/p>\n<p style=\"padding-left: 30px\">Get-ChildItem -Path c:\\fso -Recurse -Force |<\/p>\n<p style=\"padding-left: 30px\">foreach-object { $i++ }<\/p>\n<p style=\"padding-left: 30px\">$i<\/p>\n<p>&nbsp;<\/p>\n<p>Using the <b>Measure-Object<\/b> cmdlet, it is easy to count the files. I merely need to use the following steps.<\/p>\n<ol>\n<li>Use the <b>Get-Childitem<\/b> cmdlet to return a listing of <b>fileinfo<\/b> objects. Use the <i>recurse <\/i>switch to cause the cmdlet to work through subfolders. The <i>force <\/i>switch is used to return any hidden or system files. Pass the path to count to the <i>path <\/i>parameter.<\/li>\n<li>Pipe the <b>fileinfo<\/b> objects from step one to the <b>Measure-Object<\/b> cmdlet<\/li>\n<\/ol>\n<p>An example of using this command to count the files in the c:\\fso folder is shown here:<\/p>\n<p style=\"padding-left: 30px\">Get-ChildItem -Recurse -force | Measure-Object<\/p>\n<p>The command and associated output are shown in the following figure. Note that I ran the command twice: the first time without the <i>force <\/i>switched parameter, and the second time using it.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0358.hsg-10-9-11-1.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of command and associated output\" alt=\"Image of command and associated output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0358.hsg-10-9-11-1.png\" \/><\/a><\/p>\n<p>But the <b>Measure-Object<\/b> cmdlet does more than just count the number of files in a folder. It can also tell me information about a text file. A sample file is shown in the following figure.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5428.hsg-10-9-11-2.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of sample file\" alt=\"Image of sample file\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5428.hsg-10-9-11-2.png\" \/><\/a><\/p>\n<p>If I want to know how many lines are contained in the file, I use the <b>Measure-Object<\/b> cmdlet with the <i>line <\/i>switch. This command is shown here:<\/p>\n<p style=\"padding-left: 30px\">Get-Content C:\\fso\\a.txt | Measure-Object &ndash;Line<\/p>\n<p>If I need to know the number of characters, I use the <i>character <\/i>switch:<\/p>\n<p style=\"padding-left: 30px\">Get-Content C:\\fso\\a.txt | Measure-Object -Character<\/p>\n<p>There is also a <i>words <\/i>switched parameter that will return the number of words in the text file. It is used similarly to the <i>character <\/i>or <i>line <\/i>switched parameter. The command is shown here:<\/p>\n<p style=\"padding-left: 30px\">Get-Content C:\\fso\\a.txt | Measure-Object &ndash;Word<\/p>\n<p>In the following figure, I use the <b>Measure-Object<\/b> cmdlet to count lines; then lines and characters; and finally lines, characters, and words. These commands illustrate combining the switches to return specific information.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8475.hsg-10-9-11-3.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of using Measure-Object to count\" alt=\"Image of using Measure-Object to count\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8475.hsg-10-9-11-3.png\" \/><\/a><\/p>\n<p>One really cool thing I can do with the <b>Measure-Object<\/b> cmdlet is to measure specific properties of the piped objects. For example, I can use the <b>Get-ChildItem<\/b> cmdlet to return <b>fileinfo<\/b> objects for all the text files in the folder. I can examine the <b>length<\/b> property and find out the minimum length of the files in the folder, the maximum length, the average size, and the total length of all files in the folder. This command and associated output are shown here:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\fso&gt; Get-ChildItem -Filter *.txt | Measure-Object -Property length -Maximum -Minimum -Average -Sum<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Count&nbsp;&nbsp;&nbsp; : 66<\/p>\n<p style=\"padding-left: 30px\">Average&nbsp; : 305903.833333333<\/p>\n<p style=\"padding-left: 30px\">Sum&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 20189653<\/p>\n<p style=\"padding-left: 30px\">Maximum&nbsp; : 12534760<\/p>\n<p style=\"padding-left: 30px\">Minimum&nbsp; : 0<\/p>\n<p style=\"padding-left: 30px\">Property : Length<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p>If I want to, I can pipe the output to a table and create my own custom headings and output. In the following example, I display the average size of the files in kilobytes. I also define the format to omit decimal places:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\fso&gt; Get-ChildItem -Filter *.txt | Measure-Object -Property length -Maximum -Minimum -Average -Sum | ft count, @{&#8220;Label&#8221;=&#8221;average size(KB)&#8221;;&#8221;Expression&#8221;={($_.average\/1KB).tostring(0)}}<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Count&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Average size(KB)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nb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<b>Measure-Object<\/b> cmdlet is one of my favorite cmdlets because it is easy to use and extremely flexible&mdash;an unbeatable combination in my book. What is your favorite cmdlet? Add a comment below and let me know. Until tomorrow, see ya.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n<p><b><\/b>&nbsp;<\/p>\n<p><b><\/b>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to use a powerful Windows PowerShell cmdlet to count words and lines in files, or to count files. &nbsp; Microsoft Scripting Guy Ed Wilson here. The weekend is halfway over in Charlotte, North Carolina. For my friends in Australia, the weekend is already over, and they are on their way to work. [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[38,51,3,4,12,45],"class_list":["post-12451","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-storage","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to use a powerful Windows PowerShell cmdlet to count words and lines in files, or to count files. &nbsp; Microsoft Scripting Guy Ed Wilson here. The weekend is halfway over in Charlotte, North Carolina. For my friends in Australia, the weekend is already over, and they are on their way to work. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12451","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=12451"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12451\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=12451"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=12451"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=12451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}