Introduction
I needed to generate a listing of all sites within a site collection that included the size of each site. So, I put something together that could get the result done quickly. It builds upon a script foundation previously developed by Phil Childs. The link to his posting is provided below. The script presented here generates site collection size report as an HTML file. Through suitable modifications you can include other site web parameters in the listing and also get user input for other report customizations.
Script
I needed to generate a listing of all sites within a site collection that included the size of each site. So, I put something together that could get the result done quickly. It builds upon a script foundation previously developed by Phil Childs. The link to his posting is provided below. The script presented here generates site collection size report as an HTML file. Through suitable modifications you can include other site web parameters in the listing and also get user input for other report customizations.
Script
References###############################################################
# Name: Subsite Report Generator
# Author:
# Date:
# Description: enumerates all sites within the site collection
# and their sizes. Note that if you enter a path
# that includes a subsite, this will still analyze
# the entire site collection.
###############################################################
# Get the site to be analyzed
$SiteURL = Read-Host "Enter a site URL: "
# Create the report container
# ---------------------------
# Save the data and time. This date and time
# will be used also for generating the report
# filename.
$DateTime = Get-Date
# Use this date/time to generate the report file name
$DateTimeFileString = $DateTime.ToString("yyyyMMddHHmmss")
# Now generate the path/file string
$FilePathString = "D:\SubsiteReport." + $DateTimeFileString + ".html"
# Create a new text file
New-Item $FilePathString -Type File
# Add web page header
Set-Content $FilePathString "<html>"
Add-Content $FilePathString "<body>"
Add-Content $FilePathString "<head>"
Add-Content $FilePathString "<style>"
Add-Content $FilePathString "td.number{text-align:right;}"
Add-Content $FilePathString "</style>"
Add-Content $FilePathString "</head>"
# Add the date/time to the top of the file
[string]$StringToWrite = "<p> Date:" + $DateTime + "</p>"
Add-Content $FilePathString $StringToWrite
# Add a header to the report
# --------------------------
$StringToWrite = "<p>Site Collection Subsites Report</p>"
Add-Content $FilePathString $StringToWrite
$StringToWrite = "<p>Generated on host " + $env:Computername + " by " + $env:UserName + "</p>"
Add-Content $FilePathString $StringToWrite
Add-Content $FilePathString "<p>=====================================================</p>"
Add-Content $FilePathString "<table border='1'>"
Add-Content $FilePathString "<tr><th>#</th><th>Site Name</th><th>Path</th><th>Size (MB)</th></tr>"
#Get Size of all Sub-sites in a Site Collection
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$SiteNumber = 1
$TotalSize = 0
# Function to calculate folder size
Function CalculateFolderSize($Folder)
{
[long]$FolderSize = 1
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
}
$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)
$TotalSize = $TotalSize + $Size
$Size = "{0:N2}" -f $size
$StringToWrite = "<tr><td>" + $SiteNumber + "</td><td>" + $web.Name + "</td><td>" + $web.url + "</td><td class='number'>" + $Size + "</td></tr>"
Write-Host $web.Name ":`t" $Size "MB"
Add-Content $FilePathString $StringToWrite
$SiteNumber = $SiteNumber +1
#Dispose the object
$web.dispose()
}
$StringToWrite = "<tr><td colspan=4>Total size of subsites: " + $totalsize + " MB</td></tr>"
Add-Content $FilePathString $StringToWrite
Add-Content $FilePathString "</table>"
Add-Content $FilePathString "</html>"