Joe
Joe

Reputation: 1

How to get the folder sizes of top folders in shared drive and sort them from bigger to smaller

I am using get-childitem command to get the folders inside a shared drive. But when iam using the property length it is returning blank. How to sort the folder sizes by size using powershell

Command iam using Get-childitem -path $path -directory -depth 0

I have tried get childitem command.

Upvotes: 0

Views: 2514

Answers (1)

Theo
Theo

Reputation: 61028

You can either use a ForEach-Object loop, iterating the top folders in the share like this:

Get-ChildItem -Path $path -Directory | ForEach-Object {
    $size = 0 + (Get-ChildItem -Path $_.FullName -File | Measure-Object -Property Length -Sum).Sum
    [PsCustomObject]@{
        Directory = $_.Name
        Size = $size
    }
} | Sort-Object Size -Descending

Or use calculated properties with a Select-Object cmdlet like this:

Get-ChildItem -Path $path -Directory | 
Select-Object @{ Name = 'Directory'; Expression = {$_.Name}},
              @{ Name = 'Size'; Expression = { 
                0 + (Get-ChildItem -Path $_.FullName -File | Measure-Object -Property Length -Sum).Sum}
              } | 
Sort-Object Size -Descending

P.S. If you would rather have blanks instead of 0 for folders that do not have files in them, just remove the 0 +

Upvotes: 0

Related Questions