Tuesday, 17 August 2010

Check the size of SharePoint 2010 sites using PowerShell

One of those “it may come in useful one day” scripts. It walks through a site hierarchy in SharePoint 2010, reports the size of each one, and then displays a total size for all sites reported. It uses the concepts of the code specified in this article, which calculates the size of a site by adding up the contents of each folder within it.

UPDATE 29th October 2010: Modified the script to include all subfolders – thanks to Steven Wu for pointing it out.

To use, first run the following script:

function GetWebSizes ($StartWeb)
{
    $web = Get-SPWeb $StartWeb
    [long]$total = 0
    $total += GetWebSize -Web $web
    $total += GetSubWebSizes -Web $web
    $totalInMb = ($total/1024)/1024
    $totalInMb = "{0:N2}" -f $totalInMb
    $totalInGb = (($total/1024)/1024)/1024
    $totalInGb = "{0:N2}" -f $totalInGb
    write-host "Total size of all sites below" $StartWeb "is" $total "Bytes,"
    write-host "which is" $totalInMb "MB or" $totalInGb "GB"
    $web.Dispose()
}

function GetWebSize ($Web)
{
    [long]$subtotal = 0
    foreach ($folder in $Web.Folders)
    {
        $subtotal += GetFolderSize -Folder $folder
    }
    write-host "Site" $Web.Title "is" $subtotal "KB"
    return $subtotal
}

function GetSubWebSizes ($Web)
{
    [long]$subtotal = 0
    foreach ($subweb in $Web.GetSubwebsForCurrentUser())
    {
        [long]$webtotal = 0
        foreach ($folder in $subweb.Folders)
        {
            $webtotal += GetFolderSize -Folder $folder
        }
        write-host "Site" $subweb.Title "is" $webtotal "Bytes"
        $subtotal += $webtotal
        $subtotal += GetSubWebSizes -Web $subweb
    }
    return $subtotal
}

function GetFolderSize ($Folder)
{
    [long]$folderSize = 0 
    foreach ($file in $Folder.Files)
    {
        $folderSize += $file.Length;
    }
    foreach ($fd in $Folder.SubFolders)
    {
        $folderSize += GetFolderSize -Folder $fd
    }
    return $folderSize
}

Once you have run the script, you can call it with the following PowerShell command:

GetWebSizes -StartWeb <StartURL>

Using a start URL allows you to perform a size check from any site in the hierarchy – not just the top level site. In the example below, I ran it against the URL http://portal, but could also choose to run it from a sub-site, if required:

PS C:\> GetWebSizes -StartWeb http://portal
Site Intranet is 12763801 KB
Site Hello is 581759 Bytes
Site Team Site is 604175 Bytes
Total size of all sites below http://portal is 13949735 Bytes,
which is 13.30 MB or 0.01 GB

76 comments:

  1. Great article, I'll be sure to use this for client size usage chargeback in 2010. Thanks!

    ReplyDelete
  2. Will this work on MOSS 2007?

    ReplyDelete
  3. Firstly, you will need to prepare PowerShell for use with MOSS 2007. There are a few articles on this, one of them being at http://nickgrattan.wordpress.com/2007/09/03/preparing-powershell-for-sharepoint-and-moss-2007/

    Also, the Get-SPWeb cmdlet (three lines in) does not exist for MOSS 2007, so you will need to instantiate the web object in a different way. There is an article on this at http://sharepoint.microsoft.com/Blogs/zach/Lists/Posts/Post.aspx?ID=7

    Other than that, everything else should work - bearing in mind I haven't tested it of course...

    ReplyDelete
  4. Phil:
    Thanks for your effort.
    But I think you forget to consider the subFolders under folder. I do some modification for the last function. FYI.

    ----
    function GetFolderSize ($Folder)
    {
    [long]$folderSize = 0
    foreach ($file in $Folder.Files)
    {
    $folderSize += $file.Length;
    }

    foreach($fd in $Folder.SubFolders)
    {
    $folderSize += GetFolderSize -Folder $fd
    }
    return $folderSize
    }

    ReplyDelete
  5. You're right Steven - I'll update the script on the site. Thanks for the info

    ReplyDelete
  6. I copied your script into GetWebSizes.PS1, then from powershell ran .\GetWebSizes.PS1, then ran GetWebSizes -StartWeb http://server/site/
    I get: "The term 'GetWebSizes' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
    spelling of the name, or if a path was included, verify that the path is correct and try again."

    ReplyDelete
  7. I did the same as the previous user, but simply received no output - ran the script under the PS Management shell as well as standard Windows PowerShell. :-(

    ReplyDelete
  8. Hi both - I will test the script in the SharePoint Management Shell when I get a chance, but the script was written and tested using Windows PowerShell ISE included with PowerShell 2.0. See http://get-spscripts.com/2010/06/windows-powershell-ise.html for details on how to use this with SharePoint - you'll never look back :-)

    ReplyDelete
  9. Hi both (again). I tested in the SharePoint Management Shell by typing . 'c:\scripts\GetWebSizes.PS1' (note that is a full stop, then a space, and then the full script path in single quotes) and it worked for me :-)

    ReplyDelete
  10. Hey Phil
    Does this code calculate the size of 3rd or 4th level subsites also? Afer rinning this code it shows a total of 12 GB but size of DB is 40 GB any idea ??

    ReplyDelete
  11. I am having the same issue as Umar above...in SPD, it shows my site collection to be 101Gb. My SQL files are around 130Gb. And the script above reports back that the entire site collection is only 65Gb. I can't seem to figure out where the discrepency is.... Would list items (not document libraries) & list item attachments need to be somehow accounted for?

    ReplyDelete
  12. Hi Umar & Dana, the script works by calculating the size of the actual files and items stored in document libraries and lists. This will not include things like older document versions, items in the recycle bin, etc., which all contribute to the total size of the site collection/databases.

    ReplyDelete
  13. All,
    Has anyone managed to included additional major / minor versions?

    ReplyDelete
  14. Not sure why no one has mentioned this. You can use SPSite.Usage.Storage to find the size of an SPSite...

    ReplyDelete
  15. It's because SPSite.Usage.Storage tells you the size of a site collection, but not an individual site (or SPWeb)...

    ReplyDelete
  16. how can i get the report into a text file, tried ">, >> and out-file –filepath “c:\SP2010Cmdlets.txt” things but could not get, phill can you help me.

    ReplyDelete
  17. Ranjith - You will need to change the script to write to a log file instead of just using Write-Host. There is a good example here: http://www.windowsitpro.com/blog/powershell-with-a-purpose-blog-36/scripting-languages/making-a-debug-log-137465

    ReplyDelete
  18. Has anyone tried this with MySites, and if so, does it work? In our experience anything to do with MySites generally has a bunch of special considerations and exceptions to standard SP 2010 admin guidelines/tools etc. Thanks!

    ReplyDelete
  19. Not so sure this is right. I have tested this for sitecollection. The actuall size shown for the sitecollection in CA was 84MB. But when it comes to this script, its showing only 51MB.

    Where is the rest?

    ReplyDelete
  20. The additional large files that are stored in site collection database includes recycle bin items and audit trail log. I found they could be very large and significantly contribute to the site collection size. You can check the size of the AuditData and RecycleBin tables in the content database.
    Anna

    ReplyDelete
  21. it is not working for Host named site collections?

    ReplyDelete
  22. It's never been tested with Host named site collections (and never will be!)

    ReplyDelete
  23. great post.. may I suggest the following tweak which should help all of the users above...

    Create a .ps1 file and include the (awesome) script written above in this way.

    #-----------------------------------
    cls
    if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
    {
    Add-PsSnapin Microsoft.SharePoint.PowerShell
    }


    function GetWebSize ($Web)
    {
    [long]$subtotal = 0
    foreach ($folder in $Web.Folders)
    {
    $subtotal += GetFolderSize -Folder $folder
    }
    write-host "Site" $Web.Title "is" $subtotal "KB"
    return $subtotal
    }

    function GetSubWebSizes ($Web)
    {
    [long]$subtotal = 0
    foreach ($subweb in $Web.GetSubwebsForCurrentUser())
    {
    [long]$webtotal = 0
    foreach ($folder in $subweb.Folders)
    {
    $webtotal += GetFolderSize -Folder $folder
    }
    write-host "Site" $subweb.Title "is" $webtotal "Bytes"
    $subtotal += $webtotal
    $subtotal += GetSubWebSizes -Web $subweb
    }
    return $subtotal
    }

    function GetFolderSize ($Folder)
    {
    [long]$folderSize = 0
    foreach ($file in $Folder.Files)
    {
    $folderSize += $file.Length;
    }
    foreach ($fd in $Folder.SubFolders)
    {
    $folderSize += GetFolderSize -Folder $fd
    }
    return $folderSize
    }


    $web = Get-SPWeb $args[0]
    [long]$total = 0
    $total += GetWebSize -Web $web
    $total += GetSubWebSizes -Web $web
    $totalInMb = ($total/1024)/1024
    $totalInMb = "{0:N2}" -f $totalInMb
    $totalInGb = (($total/1024)/1024)/1024
    $totalInGb = "{0:N2}" -f $totalInGb
    write-host "Total size of all sites below" $StartWeb "is" $total "Bytes,"
    write-host "which is" $totalInMb "MB or" $totalInGb "GB"
    $web.Dispose()
    #--------------------------------------------


    then simply run the .ps1 file this way:
    .\GetWebSizes "URL_TO_CHECK"

    The main problem for me was that I was not loading in the funtions first. And of course make sure the SP Snap-Ins are loaded.

    -Anthony

    ReplyDelete
  24. I should mention that the .ps1 file you're creating is "GetWebSizes.ps1"

    =)

    ReplyDelete
  25. Thanks Anthony. All scripts on this site assume the SP snap-ins are already loaded and that users understand how functions work in PowerShell, but your suggestions may be helpful to newbies.

    ReplyDelete
  26. I assume this is an obvious newbie question so apologies in advance, but if I run the script and receive no output (as per anonymous above) what could I be doing wrong?

    ReplyDelete
  27. This is really strange. Is seems that powershell didn't like the filename. When I created a new script with a different name it worked fine! Thanks for a great script.

    ReplyDelete
  28. Great script - thanks for posting.

    Any idea how to get it to work for our 'My Sites'? We have these hosted on http://my but when I try that it just gives the top level site @ 8 MB, and if I try any subsite (e.g. http://my/personal/username) I get the following errors:



    PS C:\Share> GetWebSizes -StartWeb http://my/personal
    Get-SPWeb : Cannot find an SPWeb object with Id or Url : http://my/personal and site Url http://my.
    At C:\Share\SP2010-GetWebSizes.ps1:3 char:21
    + $web = Get-SPWeb <<<< $StartWeb
    + CategoryInfo : InvalidData: (Microsoft.Share....SPCmdletGetWeb:SPCmdletGetWeb) [Get-SPWeb], SPCmdletPipeBindExcepti
    on
    + FullyQualifiedErrorId : Microsoft.SharePoint.PowerShell.SPCmdletGetWeb

    The script failed due to call depth overflow. The call depth reached 1001 and the maximum is 1000.
    + CategoryInfo : InvalidOperation: (1001:Int32) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : CallDepthOverflow



    PS C:\Share> GetWebSizes -StartWeb http://my/personal/murrayb
    The following exception was thrown when trying to enumerate the collection: "0x80070005Access denied.".
    At C:\Share\SP2010-GetWebSizes.ps1:19 char:12
    + foreach <<<< ($folder in $Web.Folders)
    + CategoryInfo : NotSpecified: (:) [], ExtendedTypeSystemException
    + FullyQualifiedErrorId : ExceptionInGetEnumerator

    Site Brian Murray is 0 KB
    The following exception was thrown when trying to enumerate the collection: "0x80070005Access denied.".
    At C:\Share\SP2010-GetWebSizes.ps1:33 char:16
    + foreach <<<< ($folder in $subweb.Folders)
    + CategoryInfo : NotSpecified: (:) [], ExtendedTypeSystemException
    + FullyQualifiedErrorId : ExceptionInGetEnumerator

    Site Blog is 0 Bytes
    Total size of all sites below http://my/personal/murrayb is 0 Bytes,
    which is 0.00 MB or 0.00 GB
    PS C:\Share>

    ReplyDelete
  29. foreach ($subweb in $Web.GetSubwebsForCurrentUser())

    Why GetSubwebsForCurrentUser? Does that mean the script will tell me the wrong size if I the current user don't have access to a subweb? Not good...

    ReplyDelete
  30. ^^Don't use the script then - it makes no difference to me!

    ReplyDelete
  31. can anyone tell me how to get the subsite storage like total storage, storage warning and storage usage for subsites in a site collection.

    ReplyDelete
  32. hi Phil,
    Is there a updated script which will include the older document versions also.

    ReplyDelete
  33. Hello,

    For the people having issues running the script try to save it as .psm1 and the import the module as follows:

    Import-Module "E:\GetWebSizes.psm1"

    After this Powershell should recognize the method, so could run GetWebSizes -StartWeb without issues.

    Regards,

    Danny Quiros.

    ReplyDelete
  34. Very nice works with instructions you gave me on the link: http://get-spscripts.com/2010/06/windows-powershell-ise.html

    ReplyDelete
  35. We found with your script 50% of the actual size occupied, could you tell me if there is a strategy somewhere to find out where that other 50% is coming from? Thanks Jaap

    ReplyDelete
  36. The total will report as low if you use versioning in any of your lists or document libraries because only the size of the most recent file version is included. If you want to include the size of all versions, replace the GetFolderSize function:

    function GetFolderSize ($Folder)
    {
    [long]$folderSize = 0
    foreach ($file in $Folder.Files)
    {
    if ($file.Versions -ne $NULL -and $file.Versions.Count -gt 1) {
    foreach ($version in $file.Versions)
    {
    $folderSize += $version.Size;
    }
    }
    else {
    $folderSize += $file.Length;
    }
    }
    foreach ($fd in $Folder.SubFolders)
    {
    $folderSize += GetFolderSize -Folder $fd
    }
    return $folderSize
    }

    ReplyDelete
  37. Check these links too:

    http://blogs.technet.com/b/fromthefield/archive/2013/07/25/windows-powershell-script-to-output-site-collection-information.aspx

    http://social.technet.microsoft.com/Forums/sharepoint/en-US/4cb68370-aca3-4af4-898a-388e2874ab2f/powershell-command-to-know-the-list-of-mysites-existing-in-sharepoint-2010?forum=sharepointadminprevious

    http://sharepointquester.com/2012/06/14/reporting-sharepoint-2010-site-collections-storage-and-quota-using-windows-powershell/

    http://gallery.technet.microsoft.com/scriptcenter/Publish-Collection-Report-09e9901d

    ReplyDelete
  38. The 1000 depth recursion limit is unfortunately preventing me from getting everything using powershell version 2. :( Otherwise, this is a great little script. One modification I plan to make will have the script return a document count for each folder. This will help reconcile my webs after transferring them to a new farm.

    ReplyDelete
    Replies
    1. Great script which I would use if I needed, but for now, I only have the one site collection to worry about and I found the Storage Metrics via the interface provided extremely useful information about the size of each sub site, files and folders. Site Actions > Site Settings > Site Collection Administration > Storage Metrics. You can drill down and click through too.

      Cheers,
      Jess Wong

      Delete
  39. Does this account for any of the recycle bin storage - could be why some differences to what was expected?

    ReplyDelete
  40. Great script Phil - I've used this few times already. Thanks for putting this together!

    Just wondering whether or not this script can be modified to include the subsite URL too? I ran into a situation where there are over 1k subsites in a site collection and with just Title it's hard to track down subsites. Any help would be greatly appreciated!

    ReplyDelete
    Replies
    1. write-host "Site" $subweb.Title "at [" $subweb.ServerRelativeUrl "] is" $webtotal "Bytes"

      Delete
  41. شركة تنظيف قصور بالمدينة المنورة
    شركة نقل أثاث بالمدينة المنورة
    شركة كشف تسربات المياه بالمدينة المنورة
    شركة عزل مائى بالمدينة المنورة
    شركة عزل خزانات بالمدينة المنورة
    شركة عزل حمامات بالمدينة المنورة
    شركة عزل أسطح بالمدينة المنورة
    شركة مكافحة حشرات بالمدينة المنورة
    شركة جلى بلاط بالمدينة المنورة
    شركة تنظيف مسابح بالمدينة المنورة
    شركة تنظيف فلل بالمدينة المنورة
    فنى تركيب غرف نوم بالمدينة المنورة
    شركة نقل عفش بالمدينة المنورة
    شركة نقل عفش بالمدينة المنورة
    شركة عزل أسطح بالمدينة المنورة
    شركة تسليك مجارى بالدمام
    شركة تنظيف مسابح بالدمام
    شركة تنظيف منازل بالدمام
    شركة تنظيف مجالس بالدمام
    شركة تنظيف قصور بالدمام
    شركة تنظيف فلل بالدمام
    شركة تنظيف شقق بالدمام
    شركة تنظيف بيوت بالدمام
    شركة تنظيف بالدمام
    شركة جلى بلاط بالدمام
    شركة رش مبيدات بالدمام
    شركة مكافحة حشرات بالدمام
    شركة ركن الابداع لكافة الخدمات
    شركة مكافحة البق بجازان
    شركة مكافحة النمل الابيض بجازان
    شركة مكافحة حشرات بجازان
    شركة رش مبيدات بجازان
    شركة مكافحة البق بنجران
    شركة مكافحة النمل الابيض بنجران
    شركة مكافحة حشرات بنجران
    شركة رش مبيدات بنجران
    شركة نور الأنماء بالدمام
    شركة كشف تسربات المياه بالدمام
    شركة كشف تسربات المياه بالاحساء

    ReplyDelete
  42. شركة كشف تسربات المياه بالاحساء
    شركة كشف تسربات المياه بالجبيل
    شركة كشف تسربات المياه بالخبر
    شركة كشف تسربات المياه بالظهران
    شركة كشف تسربات المياه بالقطيف
    شركة كشف تسربات المياه بسيهات وعنك
    شركة كشف تسربات المياه ببقيق
    شركة كشف تسربات المياه بصفوى
    شركة الاتقان
    شركة كشف تسربات المياه بالاحساء
    شركة كشف تسربات المياه بالجبيل
    شركة كشف تسربات المياه بالخبر
    شركة كشف تسربات المياه بالدمام
    شركة كشف تسربات المياه بالدمام وبالاحساء
    شركة كشف تسربات المياه بالدمام وبالاحساء
    شركة كشف تسربات المياه بالظهران
    شركة كشف تسربات المياه بالقطيف
    شركة كشف تسربات المياه ببقيق
    شركة كشف تسربات المياه بسيهات وعنك
    شركة كشف تسربات المياه بصفوى
    شركة العربى أفضل شركة نظافة بالدمام والرياض
    شركة تخزين أثاث بالمدينة المنورة
    شركة تخزين عفش بالمدينة المنورة
    شركة تسليك مجارى بالمدينة المنورة
    شركة تنظيف بالمدينة المنورة
    شركة تنظيف بيوت بالمدينة المنورة
    شركة تنظيف خزانات بالمدينة المنورة
    شركة تنظيف شقق بالمدينة المنورة
    شركة تنظيف فلل بالمدينة المنورة
    شركة تنظيف قصور بالمدينة المنورة
    شركة تنظيف مجالس بالمدينة المنورة
    شركة تنظيف مسابح بالمدينة المنورة

    ReplyDelete
  43. I read your post. This is very useful. Thanks for sharing.
    seo training in chennai

    ReplyDelete
  44. I tried this in one of the Test Environment and it working really cool but bad luck it it did not work for the Prod.
    It says "Cannot fine SPSite < the site which i gave>. However the site is visible :(
    please help!!

    ReplyDelete
  45. This blog is impressive and informative.It clearly explains about the concept and its techniques.Thanks for sharing this information.Please update this type of information
    SEO Training in Chennai

    ReplyDelete
  46. This is a great tip particularly to those new to the blogosphere. Simple but very precise information… Thank you for sharing this one. A must read article!Agen Bola Sbobet Agen Bola

    ReplyDelete
  47. There are many online store in Philppinesi but Kantoph is one of the leading and trusted shopping store.online grocery store Philippines

    ReplyDelete
  48. There are many online store in Philppinesi but Kantoph is one of the leading and trusted shopping store.online grocery store Philippines

    ReplyDelete
  49. Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing.
    Regards,
    seo institute in chennai

    ReplyDelete
  50. Nice post. Thanks for sharing such a useful post.

    seo training in chennai

    ReplyDelete
  51. very very nice post


    شركة مكافحة حشرات بالمدينة المنورة


    شركة مكافحة حشرات بينبع  


    شركة تنظيف خزانات بالمدينة المنورة


    شركة تنظيف كنب بالمدينة المنورة

    شركة شراء اثاث مستعمل بالمدينه المنوره


    شراء الاثاث المستعمل بالمدينة المنورة

    شراء الاثاث المستعمل بالمدينة المنورة

    شراء الاثاث المستعمل بالمدينة المنورة

    شراء الاثاث المستعمل بالمدينة المنورة


    ارقام شراء الاثاث المستعمل بالمدينة المنورة



    شراء الاثاث المستعمل بالمدينة المنورة



    شركة تنظيف شقق بالمدينة المنورة


    شركة تنظيف كنب بالمدينة المنورة


    شركة مكافحة حشرات بالمدينة المنورة  


    شركة مكافحة حشرات بالمدينه المنوره


    شركة مكافحة حشرات بالمدينة المنورة


    شركة كشف تسربات المياة بالمدينة المنورة


    شركة نقل عفش بالمدينة المنورة    


    شركة نقل اثاث بالمدينة المنورة


    شركة تركيب غرف نوم بالمدينة المنورة


    شراء الاثاث المستعمل بتبوك


    شركة مكافحة حشرات بينبع


    شركة تسليك مجارى بالمدينة المنورة



    شركة تنظيف كنب بالمدينة المنورة


    شركة تنظيف خزانات بالمدينة المنورة


    شركة تنظيف خزانات بالمدينة المنورة


    شركة تنظيف شقق بالمدينة المنورة


    شركة كشف تسربات المياة بالمدينة المنورة


    شركة تنظيف خزانات بالمدينة المنورة


    شركة شراء الاثاث المستعمل بالمدينة المنورة


    شركة شراء اثاث مستعمل بالمدينة المنورة



    تسليك مجارى بالمدينة المنورة


    شركة غسيل كنب بالمدينة المنورة

    ReplyDelete
  52. Fixing broken air conditioning isn't good but you have to look for experts who can do repairs and maintenance.fixing broken air conditioning air conditioning informations

    ReplyDelete
  53. A service level of ninety eight% %, for instance could then be a determine
    that you just each agree for the service.

    Here My web
    - Grosir Jaket Parka
    - Grosir Jaket Parka
    - Grosir Jaket Parka
    - Grosir Jaket Parka
    - Grosir Jaket Parka
    - Grosir Jaket Parka

    ReplyDelete