Get Size of all Sub-sites in a Site Collection

Today I will post about how to obtain the size of all the subsits in a site collection, sometimes a customer ask for something like that and it’s pretty easy to obtain it.

The only work to do is to open a Sharepoint Shell, configure the url in the above code, and paste it in the shell. Then you will obtain a file with the url of each site and the total size of each them.
[void][System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”)
# Function to calculate folder size
Function CalculateFolderSize($Folder)
{
[long]$FolderSize = 0
foreach ($File in $Folder.Files)
{
#Get File Size
$FolderSize += $file.TotalLength;
#Get the Versions Size
foreach ($FileVersion in $File.Versions)
{
$FolderSize += $FileVersion.Size
}
}

#Iterate through all subfolders
foreach ($SubFolder in $Folder.SubFolders)
{
#Call the function recursively
$FolderSize += CalculateFolderSize $SubFolder
}

return $FolderSize
}

$SiteURL = “http://urlwebapp”
$Site = new-object Microsoft.SharePoint.SPSite($SiteURL)
foreach($Web in $Site.AllWebs)
{
#Call function to calculate Folder Size
[long]$WebSize = CalculateFolderSize($Web.RootFolder)
#Get Recycle Bin Size
foreach($RecycleBinItem in $Web.RecycleBin)
{
$WebSize += $RecycleBinItem.Size
}
$Size = [Math]::Round($WebSize/1MB, 2)
$web.Url+” : “+$Size+” MB” >> sitesizereport.txt
}

That’s all, hope it helps!

Advertisement

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 )

Facebook photo

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

Connecting to %s