Need help adding the size of folders with a specific name.

Hi All,

I have been assigned the task of looking through a folder recursively and only grabbing certain folders by name and then calculating the size of those folders.

So far i have been able to do this per folder and display the size of each folder on the console, but i really am struggling to find a way to sum the size of all of the folders into one figure.

I have tried to do this:

$Folders = (Get-ChildItem 'F:\Profile\' -Recurse -Force | where {$_.name -eq 'xaprofile' -or $_.name -eq 'profile' -or $_.name -eq 'lxaprofile'})

foreach ($folder in $Folders) {

$RawSize = $folder | Get-ChildItem -Recurse -Force -File | Measure-Object -Property Length -Sum

#Format to two decimal places
"{0:N2}" -f ([int32]$RawSize.sum /1MB)

}

Any steers would be much appreciated.

Thanks,

Erkan

How about a oneliner.

$PathToCheck = 'F:\Profile\'
Get-ChildItem -Path $PathToCheck -Recurse -Include xaprofile,profile,lxaprofile | ForEach-Object -Process { Get-ChildItem -Path $_.FullName -Recurse }  | Measure-Object -Property Length -Sum | Select-Object -Property @{E={"{0:N2}" -f ([int32]$_.Sum/1MB)};L='Length'

Hey, thanks for the quick response. It doesn’t seem to output anything, all i get is:

Length
------------

I also had to add an extra } on the end and remove the extra } after the first -recurse.

 

Any one?

The above code is corrected, can you try once and share the output.

Still the same, all i get is this as the output:

Length

 

Have you tried this yourself on a folder containing subfolders? Just doesn’t work as is.

Thanks,

E

 

Try this:

foreach ($folder in $Folders) {
$RawSize = $folder | Get-ChildItem -Recurse -Force -File | Measure-Object -Property Length -Sum
#Format to two decimal places
$rawsize2 = $rawsize2 + $rawsize.sum
"{0:N2}" -f ([int64]$RawSize.sum /1MB)
}
"{0:N2}" -f ([int64]$RawSize2 /1MB)

I just set another variable $rawsize2, that I build inside the loop. Let me know if its what you’re looking for.

@erkan13 Without disturbing your original logic you can do something like below

$Folders = (Get-ChildItem 'F:\Profile\' -Recurse -Force | where {$_.name -eq 'xaprofile' -or $_.name -eq 'profile' -or $_.name -eq 'lxaprofile'})
foreach ($folder in $Folders) {

$RawSize = $folder.fullname | Get-ChildItem -Recurse -Force | Measure-Object -Property Length -Sum

$hash=[PSCustomObject]@{
$($folder.name)=$("{0:N2}" -f ([int32]$RawSize.sum /1MB))
}
$hash
}


Excellent and thank you both for responding. an0nemus09’s code worked perfectly. I got nothing outputted from your code Evila. Thank you very much for taking the time to help., i don’t use Powershell enough these days and have gotten very rusty so much appreciated.

Erk