Share via


Storage Metrics using PowerShell

Using SharePoint Server 2010, 2013;

Introduction

Storage Metrics in the Site Collection Administration page can be used to access the storage consumed by all your sub-sites and libraries.It can also be retrieved using Power Shell cmdlet. The following script retrieves storage information of each sub-sites as the default feature has no option to filter the metrics data. Additional information like node level of the sub-site, last modified date, Percentage consumed from total content size and storage size in KB/MB/GB is displayed. All information is exported to a CSV format file which can be used to filter the data through Excel. The script is tested in both SharePoint Server 2010 and 2013. This would be helpful in case if we are planning for a migration from SharePoint 2010 or SharePoint 2013 and we want to know the size of each sub-sites in a site collection. We used this to report to list down all the sub-sites with size as we want to do the migration after splitting the sub-sites to different site collection as the overall size of site collection went up beyond the recommended size.

Code

#Get Size of sites and subsite in a SharePoint Site Collection
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
 
 
#Get the size of a site
function GetWebSize($Web)
{
    [long]$subtotal = 0
 
    $subtotal = GetFolderSize -Folder $Web.RootFolder
     
    $RecyclebinSize=0
    foreach($RecycleBinItem in $Web.RecycleBin)
    {
            $RecyclebinSize += $RecycleBinItem.Size
    }
       
    return $subtotal + $RecyclebinSize
} 
 
#Get the size of subsites of a site
function GetSubWebSizes($Web)
{
    [long]$subtotal = 0
    [long]$RecyclebinSize = 0
    foreach ($subweb in $Web.GetSubwebsForCurrentUser())
    {
        [long]$webtotal = 0
        $webtotal += GetFolderSize -Folder $subweb.RootFolder
        foreach($RecycleBinItem in $subweb.RecycleBin)
        {
            $RecyclebinSize += $RecycleBinItem.Size
        }
        $subtotal += $RecyclebinSize
        $subtotal += $webtotal
        $subtotal += GetSubWebSizes -Web $subweb
    }
     
    return $subtotal+$RecyclebinSize
} 
 
#Get the size of the folder
Function GetFolderSize($folder)
{
    [long]$filesize = 0;
  
    foreach ($file in $folder.Files)
    {
        $filesize += $file.TotalLength;
        
        foreach ($fileVersion in $file.Versions)
        {
            $filesize += $fileVersion.Size;
        }
    }
  
    foreach ($subfolder in $folder.SubFolders)
    {
        $filesize += GetFolderSize $subfolder
    }
            
    return $filesize;
}
 
#Gobal variable to storage the node level of a site/subsite.
$global:nodelevel = 1
 
#Function to get the node level of a subsite/site 
Function GetNodeLevel($web)
{
    if($web.ParentWeb -ne $null)
    {
        [int]$global:nodelevel++
        GetNodeLevel $web.ParentWeb
    }
    $web.Dispose()
}
 
# Function to format in KB/MB/GB
function FormatBytes ($bytes)
{
    switch ($bytes)
    {
        {$bytes -ge 1TB} {"{0:n$sigDigits}" -f ($bytes/1TB) + " TB" ; break}
        {$bytes -ge 1GB} {"{0:n$sigDigits}" -f ($bytes/1GB) + " GB" ; break}
        {$bytes -ge 1MB} {"{0:n$sigDigits}" -f ($bytes/1MB) + " MB" ; break}
        {$bytes -ge 1KB} {"{0:n$sigDigits}" -f ($bytes/1KB) + " KB" ; break}
        Default { "{0:n$sigDigits}" -f $bytes + " Bytes" }
    }
}
 
#Set Site URL variable
$siteURL = Read-Host "Enter the Site Collection URL "
 
 
#Get the Site instance
$site = Get-SPSite $siteURL
 
#Get the total storage size of the size collection
$totalSize = $site.Usage.Storage
         
#Array to hold Storage data for all sites and subsites
$StorageDataCollection = @() 
       
foreach($web in $site.AllWebs)
{
    if($web -ne $null)
    {
        #Create an object to hold storage data
        $StorageDataResult = New-Object PSObject
 
        #Get the size of site and subsites.
        $size = GetWebSize($web)
        $size+= GetSubWebSizes($web)
 
        #initialize the level variable
        $global:nodelevel = 1
         
        #Get the percentage of storage used by a specific web
        $percentage = ($size/$totalSize).tostring("P0")
 
        #Format the size in to KB/MB/GB
        $formatSize = FormatBytes $size;
         
        #To get the node level
        $val = GetNodeLevel $web;
        [string] $level = "" + $global:nodelevel
         
        #Add the properties to show in the excel file.
        $StorageDataResult | Add-Member -type NoteProperty -name "Site Name" -value $web.Title
        $StorageDataResult | Add-Member -type NoteProperty -name "URL" -value $web.Url 
        $StorageDataResult | Add-Member -type NoteProperty -name "Size" -value $formatSize
        $StorageDataResult | Add-Member -type NoteProperty -name "Last Modified Date" -value $web.LastItemModifiedDate
        $StorageDataResult | Add-Member -type NoteProperty -name "Percentage" -value $percentage 
        $StorageDataResult | Add-Member -type NoteProperty -name "Node Level" -value $level
 
        $StorageDataCollection += $StorageDataResult            
      }
      $web.Dispose()
}
       
#export the detailed storage information to a CSV file
$StorageDataCollection | sort-object "URL" | Export-csv "StorageMetricsReport.csv" -notypeinformation
Write-host "Site Storage Report has been generated!"
 
#dispose object
$site.dispose()      

     

Result

Run the script in PowerShell 

https://lh4.googleusercontent.com/_zxVq3gZy0F6c6CxiMYusu9n75Z9di_s8-wolEdhVd1m4KD8hS13IZzMfwp7QZO6OOM6tau38StvU6tktzaSz2XUZSEqeMssTInd7XXZ3cBhbBFqZzmjy6inesU6dA9-7KDGdW2O

CSV file will be created at the location where the script file has been executed from.

https://lh4.googleusercontent.com/CgIhvmj-ydVxZmsfNEbltH8tT-grBoCSIweksO-zVXPQW7jVk6AccHgigNMI4OeTc2__5kVpd3r90QvcaKrOY8_rCp4L4_KXTH8WgrCN1f7Z0JIQGgJw8b-xiLB5M3UaiBqHVDqJ