waar is de beste plaats om te kopen van voorschrift brillen http://hollandpillen.com/lorazepam/ beste otc koud geneeskunde
Jeff Wouters's Blog

PowerShell script to list folders and sizes

by on Apr.02, 2012, under PowerShell, Script

Last week @beningus, also a Dutch PowerShell interested IT Pro, contacted me for some assistance troubleshooting a script.
The purpose of that script was to list the directories of, for example, the homedirs on a fileserver in order to find the “big spenders” among the users. For this, we’ve written the following function:


function Get-FolderLength 
  # Usage: Get-FolderLength.ps1 D:\Homedirs 
  param ( [Parameter(Mandatory=$true)] [String]$Path ) 
  $FileSystemObject = New-Object -com  Scripting.FileSystemObject 
  $folders = (Get-Childitem $path | ? {$_.Attributes -eq "Directory"}) 
  foreach ($folder in $folders) 
    $folder | Add-Member -MemberType NoteProperty -Name "SizeMB" –Value(($FileSystemObject.GetFolder($folder.FullName).Size) / 1MB) 
  $folders | sort -Property SizeMB -Descending | select fullname,@{n=’Size MB’;e={"{0:N2}" –f $_.SizeMB}} 


11 Comments for this entry

  • Antonio

    Does not work. Do I need to add extra code to make it work or this is supposed to be completed code? It generates the following error:

    Get-FolderLength : Cannot bind argument to parameter ‘Path’ because it is an empty string.
    At line:14 char:17
    + Get-FolderLength <<<< $args[0]
    + CategoryInfo : InvalidData: (:) [Get-FolderLength], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Get-FolderLength

  • Jeff Wouters

    Hi Antonio.,
    Changed the code, now it works.
    Thanks for the feedback.

  • Boycie

    For me, this returns 0MB for all folders found, please help!

  • Jeff Wouters

    Hi Boycie,
    I’ve just tested the script in a newly installed VM, works for me…
    The only situations I could think of where it would give 0MB for the directories is where they are empty, have less than 0,005 MB of data in them or when there is not access to the date in the directories.
    Could you test and/or confirm this?

  • Ajit

    Jeff ,

    love your script thanks for the help

  • Hein Traag

    Hello Jeff. Can this script be modified to search for certain extensions on a fileshare and then report them by mail to the sysadmin?

  • Jeff Wouters

    Hi Hein,
    That wouldn’t be a modification but an entire new script 😉

  • Philip

    Hi Jeff

    This works great! But I’m stuck with 1 folder which does not return a size at all.
    If I do $objFSO.GetFolder($Folder.Fullname).Size it is empty … not even 0MB

  • Philip

    Maybe I forgot to mention ….
    – There’s plenty of data in this folder
    – I’ve got full access to the whole folder hierarchy

  • Jeff Wouters

    Hi Philip,
    If it works for all other folders, but not for just one then I would recommend a game of ‘look for the difference’.
    Think about folder attributes and such.
    If you find a difference (there has to be one), let me know and I can investigate / solve.

  • chewbie

    perhaps you can run it step-by step via Powershell ISE

Leave a Reply