Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display directory structure with size in Powershell

Trying to have "dir" command that displays size of the sub folders and files. After googling "powershell directory size", I found thew two useful links

  1. Determining the Size of a Folder http://technet.microsoft.com/en-us/library/ff730945.aspx
  2. PowerShell Script to Get a Directory Total Size PowerShell Script to Get a Directory Total Size

These soultions are great, but I am looking for something resembles "dir" output, handy and simple and that I can use anywhere in the folder structure.

So, I ended up doing this, Any suggestions to make it simple, elegant, efficient.

Get-ChildItem | 
Format-Table  -AutoSize Mode, LastWriteTime, Name,
     @{ Label="Length"; alignment="Left";
       Expression={ 
                    if($_.PSIsContainer -eq $True) 
                        {(New-Object -com  Scripting.FileSystemObject).GetFolder( $_.FullName).Size}  
                    else 
                        {$_.Length} 
                  }
     };  

Thank you.

like image 304
iraSenthil Avatar asked Oct 06 '10 19:10

iraSenthil


People also ask

How do I get the size of a directory in PowerShell?

You can use the Get-ChildItem ( gci alias) and Measure-Object ( measure alias) cmdlets to get the sizes of files and folders (including subfolders) in PowerShell.

How do I get a list of folder sizes?

Open a file explorer window and right-click on the 'Name' field at the top. You'll see some options – specifically, options, that let you pick what sort of info you want to see about your folders. Select Size and the property will appear on the far right of your window.

How can I list directories and their sizes in command prompt?

Specifically, du -l 1 should show the size of each subdirectory of the current directory. For more information, run du without any parameters.


1 Answers

The first minor mod would be to avoid creating a new FileSystemObject for every directory. Make this a function and pull the new-object out of the pipeline.

function DirWithSize($path=$pwd)
{
    $fso = New-Object -com  Scripting.FileSystemObject
    Get-ChildItem | Format-Table  -AutoSize Mode, LastWriteTime, Name, 
                    @{ Label="Length"; alignment="Left"; Expression={  
                         if($_.PSIsContainer)  
                             {$fso.GetFolder( $_.FullName).Size}   
                         else  
                             {$_.Length}  
                         } 
                     }
}

If you want to avoid COM altogether you could compute the dir sizes using just PowerShell like this:

function DirWithSize($path=$pwd)
{
    Get-ChildItem $path | 
        Foreach {if (!$_.PSIsContainer) {$_} `
                 else {
                     $size=0; `
                     Get-ChildItem $_ -r | Foreach {$size += $_.Length}; `
                     Add-Member NoteProperty Length $size -Inp $_ -PassThru `
                 }} |
        Format-Table Mode, LastWriteTime, Name, Length -Auto
}
like image 198
Keith Hill Avatar answered Sep 17 '22 18:09

Keith Hill