Calculate folder sizes with PowerShell

This is something I put together for a client who wanted to know how much disk space their users’ home directories were taking up on the file server before the admins went through and applied space quotas.  The file server was running Windows Server 2008 R2.

  1. #Query Folders
  2. $dirpath = Read-Host "Enter path"
  3. $subfolders = Get-ChildItem $dirpath | Where-Object {$_.PsIsContainer}
  4.  
  5. foreach ($subfolder in $subfolders)
  6. {
  7. $folder = $dirpath + "\" + $subfolder.Name
  8. $colItems = (Get-ChildItem $folder -Recurse | Measure-Object -property length -sum)
  9. $colItemsSum = ("{0:N2}" -f ($colItems.sum / 1MB) + " MB")
  10. Write-Output $folder $colItemsSum | Out-File export.txt -Append
  11. }

The script asks for a root folder (eg: D:\Users or \\servername\userhome – it works locally or remotely) and from there it finds all the subfolders one level down, calculates the size of each of the subfolders and outputs the subfolder name and calculated size to export.txt. 

If you’re only interested in subfolders which actually have something in them (ie: not interested in empty subfolders), then this code does the same thing but checks each subfolder first and ignores it if there’s nothing in it:

  1. #Query Folders
  2. $dirpath = Read-Host "Enter path"
  3. $subfolders = Get-ChildItem $dirpath | Where-Object {$_.PsIsContainer}
  4.  
  5. foreach ($subfolder in $subfolders)
  6. {
  7. $folder = $dirpath + "\" + $subfolder.Name
  8. if ($folder -ne $null){
  9. $colItems = (Get-ChildItem $folder -Recurse | Measure-Object -property length -sum)
  10. $colItemsSum = ("{0:N2}" -f ($colItems.sum / 1MB) + " MB")
  11. Write-Output $folder $colItemsSum | Out-File export.txt -Append}
  12. else {}
  13. }

The data export is a little clunky, so if anyone could suggest a better methodology (such as export to CSV with columns) I’d be really interested.  I think there’s a limitation in the fact that I’ve used “foreach” rather than “Foreach-Object” as the output from the former can’t be pipelined (or something like that) whereas the latter has more flexibility.

1 comment to Calculate folder sizes with PowerShell

  • K Lause

    Try this:

    #Query Folders
    $dirpath = Read-Host “Enter path”
    $subfolders = Get-ChildItem $dirpath | Where-Object {$_.PsIsContainer}

    foreach ($subfolder in $subfolders)
    {
    $folder = $dirpath + “\” + $subfolder.Name
    $colItems = (Get-ChildItem $folder -Recurse | Measure-Object -property length -sum)
    $colItemsSum = (“{0:N2}” -f ($colItems.sum / 1MB) + ” MB”)
    $niceformat = $folder + ” ” + $colItemsSum
    Write-Output $niceformat | Out-File export.txt -Append
    }

    It create a new variable and put the two together first then output that to the file. Gets you a nice format you can open in excel.

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>