I've been trying to write a script which will do the following:
- a.) Show the path of several folders and all of their subfolders
- b.) Show the number of files in all of these folders and subfolders
- c.) Show the size of the contents of these folders and each of their subfolders
So far, a.) and b.) have been simple, with something like the following:
$folders = @('C:\Directory1','C:\Directory2','C:\Directory3')
$output = foreach ($folder in $folders) {
Get-ChildItem $folder -Recurse -Directory | ForEach-Object{
[pscustomobject]@{
Folder = $_.FullName
Count = @(Get-ChildItem -Path $_.Fullname -File).Count
}
} | Select-Object Folder,Count
}
$output | export-csv C:\Temp\Folderinfo.csv
This worked great, but I haven't been able to get Powershell to output the folder sizes alongside the paths and numbers of files. I tried to use the Get-DirectorySize
function from this StackOverflow thread, and could only get it to output the size of the top-level folder, and never the subfolders. I have also tried passing Get-ChildItem
to Measure-Object -Property Length -sum
but ran into similar problems, where it would only show the size of the top-level folder.
Does anyone know the correct way to incorporate Measure-Object
or Get-DirectorySize
into this script, or one like it, so that it works with the needed recursion, and outputs the folder size of each path?
Thanks!