Tuesday, April 23, 2013

PowerShell Script - Get the size of the Sites

I have tested this script on SharePoint 2013 to get the size of the sub-sites with in a Site Collection.

#Get Size of all Sub-sites in a Site Collection
# 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://MySPSite/sites/portal"
$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)
    Write-Host $web.Url ":`t" $Size "MB"

Expected Output:

Note: I found it from a colleague and thought to share.