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

  40. {
  41. write - host "Invalid site collection URL.... Please check the URL" - fore cyan
  42. }
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. }
  32. 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. $scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent
  5. Set-Location $scriptBase
  6. $Output = $scriptBase + "\" + "SiteQuotaDetails.csv";
  7. "Site URL" + "," + "Owner Email" + "," + "Root Site Last Modified" + "," + "Quota Limit (MB)" + "," + "Total Storage Used (MB)" + "," + "Site
  8. Quota Percentage Used" | Out-File -Encoding Default -FilePath $Output;
  9. write-host "#############################################################" -fore cyan
  10. write-host "Enter Option 1 to get the quota details for the site collections under specific web application" -fore green
  11. write-host "Enter Option 2 to get the quota details for all the site collection in the sharepoint farm" -fore green
  12. 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. Foreach($Site in $SiteCollections)
  4. {
  5. if
  6. ($Site.Quota.Storagemaximumlevel - gt0)
  7. {
  8. [int] $MaxStorage = $Site.Quota.StorageMaximumLevel / 1MB
  9. }
  10. else
  11. {
  12. $MaxStorage = "0"
  13. };
  14. if
  15. ($Site.Usage.Storage - gt0)
    {
  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. }
  32. write - host "The output is available in "
  33. $Output " location" - fore yellow
  34. }
  35. }
  36. stop-transcript
Execution Procedure


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.