I am trying to count the files in all subfolders in a directory and display them in a list.
For instance the following dirtree:
TEST
/VOL01
Something like this should work:
dir -recurse | ?{ $_.PSIsContainer } | %{ Write-Host $_.FullName (dir $_.FullName | Measure-Object).Count }
dir -recurse
lists all files under current directory and pipes (|) the result to?{ $_.PSIsContainer }
which filters directories only then pipes again the resulting list to%{ Write-Host $_.FullName (dir $_.FullName | Measure-Object).Count }
which is a foreach loop that, for each member of the list ($_) displays the full name and the result of the following expression(dir $_.FullName | Measure-Object).Count
which provides a list of files under the $_.FullName path and counts members through Measure-Object
?{ ... }
is an alias for Where-Object
%{ ... }
is an alias for foreach