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