Powershell script to count specific files in directories & subdirectories Powershell script to count specific files in directories & subdirectories powershell powershell

Powershell script to count specific files in directories & subdirectories


Get-ChildItem -Path c:/test -recurse -filter *.xml | Group-Object -Property Directory

And to get a nicer table, you could add -NoElement to Group-Object.

To exclude some directories, try:

$excludedPaths = @("Windows", "Program Files", "Program Files (x86)");$searchPaths = Get-ChildItem -Path C:\ -Directory | Where-Object { $excludedPaths -notcontains $_.name }Get-ChildItem -Path $searchPaths -recurse -filter *.xml | Group-Object -Property Directory

Update:According to Get-ChildItem documentation, -filter is more efficient than -include when you only filter for one pattern. (If you filter for more patterns, -filter doesn't work and you have to use -include)

For excluding whole subtrees:

  • -exclude doesn't work, because it is applied to each file, it doesn't prune the whole tree, and it seems the filter is only matched to the filename, not the directory
  • your Where-Object doesn't work because $_.Directory returns nothing (not sure why, I haven't found any documentation)
  • Even if looking at another property ($_.FullName seems to do what you intend), this would only remove the directory itself, not paths starting with the directory. You would need to do a string-prefix-match (using -imatch or -ilike) against each string in the filter-set