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
Great article, I'll be sure to use this for client size usage chargeback in 2010. Thanks!
ReplyDeleteWill this work on MOSS 2007?
ReplyDeleteFirstly, 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/
ReplyDeleteAlso, 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...
Phil:
ReplyDeleteThanks 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
}
You're right Steven - I'll update the script on the site. Thanks for the info
ReplyDeleteI copied your script into GetWebSizes.PS1, then from powershell ran .\GetWebSizes.PS1, then ran GetWebSizes -StartWeb http://server/site/
ReplyDeleteI 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."
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. :-(
ReplyDeleteHi 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 :-)
ReplyDeleteHi 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 :-)
ReplyDeleteHey Phil
ReplyDeleteDoes 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 ??
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?
ReplyDeleteHi 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.
ReplyDeleteAll,
ReplyDeleteHas anyone managed to included additional major / minor versions?
Not sure why no one has mentioned this. You can use SPSite.Usage.Storage to find the size of an SPSite...
ReplyDeleteIt's because SPSite.Usage.Storage tells you the size of a site collection, but not an individual site (or SPWeb)...
ReplyDeletehow 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.
ReplyDeleteRanjith - 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
ReplyDeleteHas 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!
ReplyDeleteNot 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.
ReplyDeleteWhere is the rest?
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.
ReplyDeleteAnna