Author Posts

February 6, 2017 at 10:08 pm

I am trying to write a PowerShell command to return the size of each immediate sub-folder. The script should return the size of the size of the file layer of subfolders and for each subfolder the sum of all of its decedents. I have written the following and it give me the info, however in doing so I lost the folder name property.
gci -Directory | ForEach-Object {Get-ChildItem -file $_ -Recurse | Measure-Object -sum length} |Format-Table
Namely I need the name first layer displayed along side the sum of length of all of its subsequent decedents.

How do I achieve this?

February 7, 2017 at 10:48 am

The following could work for you:

Get-ChildItem -Directory | ForEach-Object {
    [PSCustomObject] @{
        FolderPath = $_.FullName
        FolderSize = Get-ChildItem -Path $_.FullName -File -Recurse -ErrorAction SilentlyContinue | 
            Measure-Object -Sum Length | Select-Object -ExpandProperty Sum

February 7, 2017 at 10:08 pm

Good stuff Daniel, thanks, that is exactly what I need.