Caution trying to get FolderSize information w/Powershell…

Make sure to roughly test all Powershell script you may download from the Internet (even the ones I’m putting in my blog).  I just found a couple of sample that will help display a list folder size (which Windows Explorer won’t provide).  Unfortunately the results displayed wasn’t perfect.  I had to use Windows Explorer to verify size information by doing the right-click on the folder to compare the results.  Is this a bug? or just a mistake when creating complex cmdlet.

The problem is that, in most cases, the folders listed are fine.  I only caught this issue when listings folders that contains lots of subfolders. 

Here are some Powershell samples:

1. Powershell (PS) script code from the Internet: (building a function)

Function getFolderSize{
$items = Get-Childitem $args
foreach($item in $items){
if ($item -is [System.IO.DirectoryInfo]){
    Add-member -inputobject $item -membertype ScriptProperty -Name Length -Value{$a = `
    get-childitem $this.FullName -include *.* -recurse | measure-object -sum Length;
    If($a -eq $null){return 0;} else {return $a.sum}}
    write-output $item
    }
}
}

This code work but still doesn’t give you 100% correct information(maybe about 80%).

2. Another PS script I retouched from another source: (building a function

Function DisplayFolderSize {
$folderTree=get-childitem $args;
[double] $fsize = 0;
[double] $fsz = 0;
[double] $TotalFSZ = 0;
Write-Host "";
Write-Host "Get Foldersize for $Args";
write-host "————————";
foreach ($folt in $folderTree) {
    if($folt.mode -match "d") {         
         $fsz=((get-childItem $folt.fullname -recurse | Measure-object length -sum).sum);
         $fsize="{0:N2}" -f $fsz;
         write-host ("$fsize `t `t `t `t $folt ");
         [string] $FolderRecords = $FolderRecords + $folt.name + "," + $fsize + "`r`n";
        $TotalFSZ=$TotalFSZ+$fsize;               
    }
  }
write-host "————————";
write-host "Total Foldersize";
write-host $TotalFSZ;
write-host "";
}

This code gave me better result but still not a 100% correct information (maybe 90%).

3. Now, here’s the winner.  I created this PS script using the "Scripting.FileSystemObject" COM object.  This way I can create a collection of members for the folder I’m querying the information. (notice this is not a function)
The following lines you can execute line at a time or just create a PS script file or create a function.

###############################################
## objFolderSize.ps1                                              ##
## Author: Max Trinidad, 02/07/2008                    ##
###############################################

$FolderName = Read-Host -Prompt "Enter Foldername: "
$FileSystem = new-object -com "Scripting.FileSystemObject";
$getFolderAttr = $FileSystem.GetFolder($FolderName);
$getSubFolderAttr = $getFolderAttr.SubFolders
$getSubFolderAttr | select name, size | ft -auto

## end of script##This code gave me a 100% correct information.
 

I did gather the results of all three PS scripts and copy/paste them into Excel.  Then I verify the folder size result against Windows Explorer making the #3 PS Script the winner.

Did you notice how many lines of code I use in PS script #3 ?   🙂

Happy coding!!
Advertisements

About maxt2posh

I’m from Puerto Rico, have been working with computers since 1979. Throughout many years, I have worked with SQL Server Technologies, provided support to Windows Servers/Client machines, Microsoft Virtualization Technologies, and build some Visual Studio solutions. I’m a Microsoft PowerShell MVP since 2009 and MVP SAPIEN Technologies since 2010. I speak in most of the SQLSaturday, IT Pro and .NET camps events around the Florida’s State. Also founder of the Florida PowerShell User Group which meeting every 3rd Tuesday evening of the month.
This entry was posted in PowerShell. Bookmark the permalink.

17 Responses to Caution trying to get FolderSize information w/Powershell…

  1. Anonymous says:

    Is there any way to get the 3rd script to display the file lengths in mb or gb?

  2. bjornwork says:

    Hello, and thank you for a great article! As the poster above, I also have problems converting script no 3 as well to megabytes.

    Can it be done in an easy manner?

    Thanks!

  3. Anonymous says:

    #Change KB to MB or GB
    $FolderName = Read-Host -Prompt “Enter Foldername: ”
    $FileSystem = new-object -com “Scripting.FileSystemObject”;
    $getFolderAttr = $FileSystem.GetFolder($FolderName);
    $getSubFolderAttr = $getFolderAttr.SubFolders
    $getSubFolderAttr | select path, @{Name=”size(KB)”;Expression={“{0:N2}” -f($_.size/1KB)}} | ft -auto

  4. Pingback: ps and folder size « IT Info

  5. Anonymous says:

    I’ve have developed a few PS scripts which does the job but my only hurdle was SPEED, particularly when scanning a large directory (e.g. C:\Program Files, C:\Windows etc.) across a remote server. Well this script is not only accurate but damn FAST. Great job!

  6. kagirohi.net says:

    Hello There. I found your weblog the usage of msn.
    That is an extremely well written article.
    I will make sure to bookmark it and come back to read
    extra of your helpful information. Thanks for the post.
    I’ll certainly return.

  7. Anonymous says:

    Is there a way to sum up the export of size?

  8. Because the admin of this web site is working, no uncertainty very quickly it will be renowned,
    due to its feature contents.

  9. Erma says:

    Yes! Finally something about mlm business opportunity.

  10. Spot on with this write-up, I actually feel this site needs a great deal more attention. I’ll probably be back again to read more, thanks for the advice!

  11. Hiya! Quick question that’s totally off topic.
    Do you know how to make your site mobile friendly? My blog looks weird
    when viewing from my iphone 4. I’m trying to find a theme or plugin that might be
    able to correct this problem. If you have any recommendations, please share.
    With thanks!

  12. Amazing blog! Do you have any tips and hints for aspiring writers?
    I’m planning to start my own website soon but I’m a little lost on everything.
    Would you recommend starting with a free platform like WordPress or go for a paid option? There
    are so many choices out there that I’m totally overwhelmed ..
    Any suggestions? Thank you!

  13. wonderful submit, very informative. I ponder why the other specialists of this sector do not notice this.
    You must proceed your writing. I’m confident, you have a great readers’ base already!

  14. This is very interesting, You are a very skilled blogger.
    I have joined your rss feed and look forward to seeking more of your excellent post.

    Also, I’ve shared your site in my social networks!

  15. I know this if off topic but I’m looking into starting
    my own weblog and was curious what all is required
    to get set up? I’m assuming having a blog like yours would cost a
    pretty penny? I’m not very web savvy so I’m not 100% sure.
    Any recommendations or advice would be greatly appreciated.
    Many thanks

  16. Magnificent goods from you, man. I have consider
    your stuff previous to and you’re simply too magnificent.
    I really like what you have received here, certainly like what you’re saying and the way in which by which you assert it.
    You make it entertaining and you continue to take care of to keep it
    sensible. I can not wait to learn much more from you. This is actually a wonderful site.

  17. whistling viking says:

    I am fairly new to powershell. Is there a way to modify #3 to show the total size of the root folder with all the other subfolders added in?

    ie. c:\windows = 2 GB
    instead of
    c:\windows\system32 = 1 GB
    c:\windows\syswow64= 750 MB
    c:\windows\inf= 250MB

    Thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s