top of page

Use Powershell script to count the number of lines in multiple text files

You can use a PowerShell script to count the number of lines in multiple text files saved to a folder.



Enter the file path for the folder after the Get-ChildItem command on the first line. Then specify the extension of the files to be analyzed towards the end of the first line after 'extension - eq'.


Get-ChildItem c:\foofolder\test2 -recurse | where {$_.extension -eq ".txt"} | % {

$_ | Select-Object -Property 'Name', @{

label = 'Lines'; expression = {

($_ | Get-Content).Length

}

}

} | out-file C:\foofolder\test2\lines1.txt

On the last line provide the file path for a new text file to which PowerShell will write the results. Open Windows PowerShell ISE (x86) and then enter the script in a new pane, and then press the play button on the toolbar.



The text file that is generated will list each file name in the source folder and show the number of lines in each in a column to the right.












I ran this script on a set of more than 100,000 text files (which turned out to consist of more than 9 million lines) and it finished the review in less than 30 minutes.


The script can also be used to find the number of lines in other files such as .csv files.


Be sure to enter the file paths in quotes if they include blank spaces.


Thanks to Hari Parkash for posting this script here.


Comentários


bottom of page