Get Size of sub sites PowerShell Script SharePoint 2013 or SharePoint 2010


We were having a requirement to get each site collection and subsites size throughout the web application in SharePoint 2013.

Below PowerShell script can be used to get the size of sites and subsites.

Create sitesInputs.csv file to keep site URL and Siteize.csv to keep the site size details.

You can place PS file and CSV files inside one folder.

cls
#———————————–
[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”) > $null

$LogDir = $(“$dp0\”)

#The datatable which contains all the User information
$CSVDtTb = New-Object system.Data.DataTable “tabName”
$isLogFileCreated = $False

Function CreateCSVDtTb()
{

if(!$isLogFileCreated){
#Define Columns

$dcObjUrl = New-Object system.Data.DataColumn SiteUrl,([string])
$dcSizeinMB = New-Object system.Data.DataColumn SizeinMB,([string])
$dcSizeinGB = New-Object system.Data.DataColumn SizeinGB,([string])

#Add the Columns
$CSVDtTb.columns.add($dcObjUrl)
$CSVDtTb.columns.add($dcSizeinMB)
$CSVDtTb.columns.add($dcSizeinGB)
$script:isLogFileCreated = $True
}
}

Function AddRow([string] $SiteUrl,[string] $SizeinMB,[string] $SizeinGB )
{
#Create a row
$row = $CSVDtTb.NewRow()

#Enter data in the row
$row.SiteUrl = $SiteUrl
$row.SizeinMB = $SizeinMB
$row.SizeinGB = $SizeinGB

#Add the row to the table
$CSVDtTb.Rows.Add($row)
}

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
}
# loop through all the subsites until nth level
function GetSubWebSizes ($Web)
{
[long]$subtotal = 0
foreach ($subweb in $Web.Webs)
{
[long]$webtotal = 0
foreach ($folder in $subweb.Folders)
{
$webtotal += GetFolderSize -Folder $folder
}
$subtotal += $webtotal
$subtotal += GetSubWebSizes -Web $subweb
}
return $subtotal
}
# loop through all the folder level
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
}



Function GenerateSiteInfo($TargetSiteUrl)
{

#Start Script Execution

CreateCSVDtTb

#Create a variable based on the current date and time
$StartTime = (Get-Date -UFormat “%Y-%m-%d_%I-%M-%S %p”).tostring()
$CsvReportFile = “$LogDir\Siteize.csv”
Write-Host “Start Processing…”

#Get all sites within each of the specified web application
$site = new-Object Microsoft.SharePoint.SPSite($TargetSiteUrl)
$TargetWeb = $site.OpenWeb()

[long]$total = 0

$total += GetWebSize -Web $TargetWeb
$total += GetSubWebSizes -Web $TargetWeb

$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”

AddRow -SiteUrl $row.SiteUrl -SizeinMB $totalInMb -SizeinGB $totalInGb
$site.Dispose()

$CSVDtTb | Export-Csv $CsvReportFile -NoTypeInformation

Write-Host “End Processing…”

}

#——————————————–

function GetAllSites ($CsvFilePath)
{
write-host “Process Started ….”

$CSVData = Import-CSV -Path $CsvFilePath

#loop through all the siteURLS in CSV file
foreach($row in $CSVData)
{
try
{
write-host “Fetching the Site Size : ” $row.SiteUrl;
GenerateSiteInfo($row.SiteUrl)
}
catch
{
write-host $row.SiteUrl “does not exist…” +$_.Exception.Message -BackgroundColor DarkGray
}
}

write-host “Process Ended ….”
}
GetAllSites($LogDir+”\sitesInputs.csv”)

Read some SharePoint PowerShell tutorials:


Hope this article will be helpful.

Check out Best Alternative to InfoPath -> Try Now

You May Also like the Following SharePoint Online Tutorials:

About Sambita Rath

I have 9 years of exprience into SharePoint implementation, architecture, Administrator, development and Training.Designing Information Architecture in SharePoint 2007, 2010,2013 and Office 365.

View all posts by Sambita Rath →