Powershell script for counting specific files in directories and subdirectories

I am trying to make a powershell script that will read all .xml files in directories and subdirectories and list the value + path.

So far I have done the following:

Get-ChildItem -Path c:/test -recurse -include *.xml

Which is close to what I want, but without the actual file names, just the paths and number of folders.

This is what I get:

>     Directory: C:\test\A_Sub-folder
> Mode                LastWriteTime     Length Name
> ----                -------------     ------ ----
> -a---        27/11/2015     11:29          0 AA.xml
> 
>     Directory: C:\test 
> Mode                LastWriteTime     Length Name
> ----                -------------     ------ ----
> -a---        27/11/2015     11:29          0 BB.xml
> -a---        27/11/2015     11:29          0 CC.xml
> -a---        27/11/2015     11:29          0 DD.xml

And I'm trying to get this (or similar):

 >  Directory: C:\test\A_Sub-folder
 > 1
 >  Directory: C:\test 
 > 3

The plan is to run this script on each root drive (some drives have about 5k.xml files, so I'm not sure how this will affect performance.)

Edit:

This works fine for subfolders, but for some reason it does not work in root directories (for example, e: /). I am trying to exclude \ windows and \ program files, but this does not work. Is there any way to exclude root directories in a search?

Script:

$excludedPaths = @("E:\Windows", "E\Program Files", "E:\Program Files (x86)", "E:\MSSQL", "E:\MSSQL11.MSSQLSERVER");
$pathtocount = Read-Host -Prompt 'Input path to count xml files'
Get-ChildItem -Path $pathtocount -recurse -include *.xml | Where-Object { $excludedPaths -notcontains $_.Directory } | Group-Object -Property Directory | Sort-Object count
+4
1
Get-ChildItem -Path c:/test -recurse -filter *.xml | Group-Object -Property Directory

, -NoElement Group-Object.

, :

$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: Get-ChildItem, -filter , -include, . ( , -filter , -include)

:

  • -exclude , , , , ,
  • Where-Object , $_.Directory ( , , )
  • ($_.FullName , ), , , . string-prefix-match ( -imatch -ilike)
+6

Source: https://habr.com/ru/post/1618613/


All Articles