02 Apr 2012 @ 9:47 AM 

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}} 
}

 

Posted By: Jeff Wouters
Last Edit: 03 Jul 2012 @ 03:13 PM

EmailPermalink
Tags
Categories: PowerShell, Script


  • 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

  • http://www.jeffwouters.nl Jeff Wouters

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

  • Boycie

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

  • http://www.jeffwouters.nl 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?
    Jeff.

  • 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?

  • http://www.jeffwouters.nl Jeff Wouters

    Hi Hein,
    That wouldn’t be a modification but an entire new script ;-)
    Jeff.


 Last 50 Posts
 Back
Change Theme...
  • Users » 1
  • Posts/Pages » 322
  • Comments » 462
Change Theme...
  • VoidVoid « Default
  • LifeLife
  • EarthEarth
  • WindWind
  • WaterWater
  • FireFire
  • LightLight

About



    No Child Pages.

Contact



    No Child Pages.

Speaking



    No Child Pages.

Health Check



    No Child Pages.