Site Quota

A quota specifies the storage limit values for the maximum amount of data that can be stored in a site collection. Quotas also specify the storage size that, when reached, triggers an e-mail alert to the site collection administrator. Quotas can be saved as quota templates that can then be applied to any site collection in a SharePoint Farm. Using quota templates rather than individual quotas can simplify setting storage limits on new site collections.

The storage limit applies to the site collection as a whole. In other words, the storage limit applies to the total size of the content for the top-level site and for all subsites within the site collection. If versioning is enabled, the versions in a site and the content in the Recycle Bins count toward storage limits. You can also specify a percentage of storage limits for the second-stage Recycle Bin.

The script collects the following information:

  1. Site URL
  2. Site owner email
  3. Site last modified date
  4. Maximum storage
  5. Used storage
  6. Site quota used
Sites under specific web app

The following piece of code gets the quota details for all the sites under a specific webapplication:
  1. $Siteurl = read - host "Enter any site collection URL under the specific web application "  
  2.   
  3. $TestSiteCollectionExists = get - spsite $Siteurl  
  4.   
  5. if ($TestSiteCollectionExists - ne $null)  
  6.   
  7. {  
  8.   
  9.     $Rootweb = New - Object Microsoft.Sharepoint.Spsite($Siteurl);  
  10.   
  11.     $Webapp = $Rootweb.Webapplication;  
  12.  
  13.     #Loops through each site collection within the Web app,  
  14.     if the owner has an e - mail address this is written to the output file  
  15.   
  16.     Foreach($Site in $Webapp.Sites)  
  17.   
  18.     {  
  19.   
  20.         if  
  21.   
  22.         ($Site.Quota.Storagemaximumlevel - gt0)  
  23.   
  24.         {  
  25.             [int] $MaxStorage = $Site.Quota.StorageMaximumLevel / 1MB  
  26.         } 
  27.  else   
  28.         {   
  29.             $MaxStorage = "0"  
  30.         };   
  31.  if  
  32.         ($Site.Usage.Storage - gt0)
            {  
  33.         [int] $StorageUsed = $Site.Usage.Storage / 1MB  
  34.         };   
  35.  if   
  36.         ($Storageused - gt0 - and $Maxstorage - gt0)   
  37.         {  
  38.             [int] $SiteQuotaUsed = $Storageused / $Maxstorage * 100  
  39.         }
     else   
  40.         {  
  41.             $SiteQuotaUsed = "0"  
  42.         };  
  43.         $Web = $Site.Rootweb;  
  44.         $Site.Url + "," + $Site.Owner.Email + "," + $Web.LastItemModifiedDate.ToShortDateString() + "," + $MaxStorage + "," + $StorageUsed + "," + $SiteQuotaUsed | Out - File - Encoding Default - Append - FilePath $Output;   
  45.         $Web.Dispose();   
  46.         $Site.Dispose()  
  47.     }   
  48.     write - host "The output is available in "  
  49.     $Output " location" - fore yellow   

  50. else
       
  51. {   
  52.     write - host "Invalid site collection URL.... Please check the URL" - fore cyan   

Sites under SharePoint Farm

The following piece of code gets the quota details for all the site collections under the SharePoint Farm:
  1. $SiteCollections = get - spsite  
  2. Foreach($Site in $SiteCollections)  
  3. {  
  4.     if  
  5.     ($Site.Quota.Storagemaximumlevel - gt0)   
  6.     {  
  7.         [int] $MaxStorage = $Site.Quota.StorageMaximumLevel / 1MB  
  8.     }
  9.     else  
  10.     {  
  11.         $MaxStorage = "0"  
  12.     };  
  13.     if
  14.     ($Site.Usage.Storage - gt0)
  15.     {  
  16.         [int] $StorageUsed = $Site.Usage.Storage / 1MB  
  17.     };  
  18.     if  
  19.     ($Storageused - gt0 - and $Maxstorage - gt0)
  20.     {  
  21.         [int] $SiteQuotaUsed = $Storageused / $Maxstorage * 100  
  22.     }
  23.     else  
  24.     {  
  25.         $SiteQuotaUsed = "0"  
  26.     };  
  27.     $Web = $Site.Rootweb;  
  28.     $Site.Url + "," + $Site.Owner.Email + "," + $Web.LastItemModifiedDate.ToShortDateString() + "," + $MaxStorage + "," + $StorageUsed + "," + $SiteQuotaUsed | Out - File - Encoding Default - Append - FilePath $Output;   
  29.     $Web.Dispose();  
  30.     $Site.Dispose()  

  31. write-host "The output is available in " $Output " location" -fore yellow
Complete code
  1. $LogTime = Get-Date -Format yyyy-MM-dd_hh-mm  
  2. $LogFile = ".\SiteCollectionQuotaDetailsPatch-$LogTime.rtf"  
  3. start-transcript $logfile 
# Add SharePoint PowerShell Snaping
  1. if( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )  
  2. {  
  3. Add-PSSnapin Microsoft.SharePoint.Powershell  
  4.    
  5. $scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent  
  6. Set-Location $scriptBase  
  7. $Output = $scriptBase + "\" + "SiteQuotaDetails.csv";  
  8. "Site URL" + "," + "Owner Email" + "," + "Root Site Last Modified" + "," + "Quota Limit (MB)" + "," + "Total Storage Used (MB)" + "," + "Site  
  9. Quota Percentage Used" | Out-File -Encoding Default -FilePath $Output;  
  10. write-host "#############################################################" -fore cyan  
  11. write-host "Enter Option 1 to get the quota details for the site collections under specific web application" -fore green  
  12. write-host "Enter Option 2 to get the quota details for all the site collection in the sharepoint farm" -fore green  
  13. write-host "#############################################################" -fore cyan 
#Switch case

1
  1. {
  2.     $Siteurl = read - host "Enter any site collection URL under the specific web application "  
  3.     $TestSiteCollectionExists = get - spsite $Siteurl  
  4.     if ($TestSiteCollectionExists - ne $null)  
  5.     {  
  6.         $Rootweb = New - Object Microsoft.Sharepoint.Spsite($Siteurl);
  7.         $Webapp = $Rootweb.Webapplication;  
  8.         #Loops through each site collection within the Web app,  
  9.         if the owner has an e - mail address this is written to the output file  
  10.         Foreach($Site in $Webapp.Sites)  
  11.         {  
  12.             if  
  13.             ($Site.Quota.Storagemaximumlevel - gt0)  
  14.             {  
  15.                 [int] $MaxStorage = $Site.Quota.StorageMaximumLevel / 1MB  
  16.             }
  17.             else
  18.             {  
  19.                 $MaxStorage = "0"  
  20.             };  
  21.             if  
  22.             ($Site.Usage.Storage - gt0)
  23.             {  
  24.                 [int] $StorageUsed = $Site.Usage.Storage / 1MB  
  25.             };  
  26.             if  
  27.             ($Storageused - gt0 - and $Maxstorage - gt0)  
  28.             {  
  29.                 [int] $SiteQuotaUsed = $Storageused / $Maxstorage * 100  
  30.             }
  31.             else  
  32.             {  
  33.                 $SiteQuotaUsed = "0"  
  34.             };  
  35.             $Web = $Site.Rootweb;  
  36.             $Site.Url + "," + $Site.Owner.Email + "," + $Web.LastItemModifiedDate.ToShortDateString() + "," + $MaxStorage + "," + $StorageUsed + "," + $SiteQuotaUsed | Out - File - Encoding Default - Append - FilePath $Output;  
  37.             $Web.Dispose();  
  38.             $Site.Dispose()  
  39.         }  
  40.         write - host "The output is available in "  
  41.         $Output " location" - fore yellow  
  42.     }
  43.       else  
  44.     {  
  45.         write - host "Invalid site collection URL.... Please check the URL" - fore cyan  
  46.     }  
  47.   

2
  1. {  
  2.     $SiteCollections = get - spsite  
  3.   
  4.     Foreach($Site in $SiteCollections)  
  5.   
  6.     {  
  7.         if
  8.         ($Site.Quota.Storagemaximumlevel - gt0)  
  9.         {  
  10.             [int] $MaxStorage = $Site.Quota.StorageMaximumLevel / 1MB  
  11.         }
  12.         else  
  13.         {
  14.             $MaxStorage = "0"  
  15.         };  
  16.         if  
  17.         ($Site.Usage.Storage - gt0)
            {  
  18.             [int] $StorageUsed = $Site.Usage.Storage / 1MB  
  19.         };  
  20.         if  
  21.         ($Storageused - gt0 - and $Maxstorage - gt0)  
  22.         {  
  23.             [int] $SiteQuotaUsed = $Storageused / $Maxstorage * 100  
  24.         } 
  25.         else
  26.         {  
  27.             $SiteQuotaUsed = "0"  
  28.         };  
  29.         $Web = $Site.Rootweb;  
  30.         $Site.Url + "," + $Site.Owner.Email + "," + $Web.LastItemModifiedDate.ToShortDateString() + "," + $MaxStorage + "," + $StorageUsed + "," + $SiteQuotaUsed | Out - File - Encoding Default - Append - FilePath $Output;  
  31.         $Web.Dispose();  
  32.         $Site.Dispose()  
  33.     }  
  34.     write - host "The output is available in "  
  35.     $Output " location" - fore yellow  
  36.   }  
  37.   

  38. stop-transcript
Execution Procedure
  • Step 1: Download and copy the script to the SharePoint server
  • Step 2: Navigate to the script path
  • Step 3: Execute the script


Enter option “1” or “2” to get the desired output.

Conclusion

Thus this article outlines how to get the site quota details for SharePoint sites using a PowerShell script.

Next Recommended Readings