Author Posts

January 1, 2012 at 12:00 am

by sukans1995 at 2013-05-03 13:21:13

I would like an easy way to find out for a given path along with filters like "c:\windows|*.txt" as a parameter, the number of lines in each file, along with filename

Output will be like

Filename Total lines
===========================
name1.txt 300
name2.txt 10
name3.txt 40

Thanks
Sukanya

by ArtB0514 at 2013-05-03 13:50:27

#requires PowerShell V3.0
$FileData = @()
Get-ChildItem c:\Windows -Include *.txt | ForEach-Object {
$FileData += [pscustomobject]@{'FileName'=$_.Name;'Total Lines'=(Get-Content $_.FullName).Count}
}

or this for any version of PowerShell:
$FileData=@()
Get-ChildItem C:\Windows -Include *.txt | ForEach-Object {
$FileData += New-Object PSObject -Property @{'FileName'=$_.Name;'Total Lines'=(Get-Content $_.FullName).Count}
}