SharePoint Online: Monitor Site Collection Storage Usage with PowerShell
Requirement: Monitor SharePoint Online storage metrics and Send Emails on current usage.
SharePoint Online: Storage Usage Report using PowerShell
By default, SharePoint Online storage management is set to “Automatic,” and all storage allocations are automatically managed from the central pool of storage. However, In your environment, if the storage management option is set to “manual” to fine-tune the storage space allocated to each site, then It’s a good idea to set up an alert email so that you are notified when sites are nearing the storage limit to make sure they aren’t affecting site performance.
Although you can check the storage consumption from Microsoft 365 usage reports, storage metrics page of SharePoint sites, or SharePoint Online Admin center, let’s automate that process! Generate a storage metrics report and Send an Email to SharePoint Administrators using PowerShell.
#Set Parameters
$AdminCenterURL="https://crescent-admin.sharepoint.com"
$AdminUserName = "Salaudeen@Crescent.com"
$AdminPassword = "Password Goes here"
$EmailFrom = "Salaudeen@Crescent.com"
#Prepare the Credentials
$SecurePassword = ConvertTo-SecureString $AdminPassword -AsPlainText -Force
$Credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminUserName, $SecurePassword
#Connect to SharePoint Online
Connect-SPOService -url $AdminCenterURL -Credential $Credential
#Get All Site Collections Site Storage Usage
[string]$SiteStorage = Get-SPOSite -Limit All | Select Title, URL, StorageQuota, StorageUsageCurrent, @{Label="Percentage";Expression={[math]::Round( ($_.StorageUsageCurrent / $_.StorageQuota * 100),2)}} | ConvertTo-Html
#Send Email
Send-MailMessage -From $EmailFrom -To $AdminUserName -Subject "Storage Report" -Body $SiteStorage -BodyAsHtml -smtpserver "smtp.office365.com" -usessl -Credential $Credential -Port 587
PnP PowerShell to Monitor Site Storage and Send Email
The built-in storage quota warning emails are typically sent on a weekly basis when sites reach the specific warning level. However, those Emails could be too late, and sites could reach the maximum storage limit and be set to read-only before the site admin receives the alert email. So, Here is the PnP PowerShell script to monitor storage usage and send alert emails.
#Set Variables
$TenantAdminURL = "https://crescent-admin.sharepoint.com"
$EmailTo = "Salaudeen@crescent.com"
$AdminUserName = "Salaudeen@crescent.com"
$AdminPassword = "Password goes here"
#Prepare the Credentials
$SecurePassword = ConvertTo-SecureString $AdminPassword -AsPlainText -Force
$Credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminUserName, $SecurePassword
#Connect to PnP Online
Connect-PnPOnline -Url $TenantAdminURL -Credentials $Credential
$CSSStyle = "<style>
table {font-family: Arial, Helvetica, sans-serif; border-collapse: collapse; width: 100%;}
table td, th {border: 1px solid #ddd; padding: 8px;}
table tr:nth-child(even){background-color: #f2f2f2;}
table tr:hover {background-color: #ddd;}
table th { padding-top: 12px; padding-bottom: 12px; text-align: left; background-color: #4CAF50; color: white;}
</style>"
#Get the Site collection Storage details
[string]$EmailBody = Get-PnPTenantSite | Select @{Label="Title";Expression={$_.Title}},
@{Label="URL";Expression={$_.URL}},
@{Label="Allocated";Expression={$_.StorageMaximumLevel}},
@{Label="Used";Expression={$_.StorageUsage}},
@{Label="Percentage";Expression={[math]::Round( ($_.StorageUsage/$_.StorageMaximumLevel*100),2)}} |
ConvertTo-Html -Title "Crescent Inc. Storage Report" -Head $CSSStyle -PreContent "Sites Storage Report"
#Send Email
Send-PnPMail -To $EmailTo -Subject "Storage Report" -Body $EmailBody
And here is the script in action:
Please note, although I’ve used plain passwords in the script, the recommended approach is using App ID – App Secret to authenticate in SharePoint Online! For SharePoint Online Management Shell, You can use an encrypted password file! Once the script is ready, you can schedule it to run once per day with Windows task scheduler or Azure Automation.
Get Alert Emails when Sites Exceed a Threshold Value
What if you want to get notified only when sites exceed a particular threshold value in storage consumption? Say you want to get alert emails when sites reach 90% of allocated storage.
#Set Variables
$TenantAdminURL = "https://crescent-admin.sharepoint.com"
$EmailTo = "Salaudeen@crescent.com"
$AdminUserName = "Salaudeen@crescent.com"
$AdminPassword = "Password goes here"
$PercentageThreshold = 90
#Prepare the Credentials
$SecurePassword = ConvertTo-SecureString $AdminPassword -AsPlainText -Force
$Credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminUserName, $SecurePassword
#Connect to PnP Online
Connect-PnPOnline -Url $TenantAdminURL -Credentials $Credential
$CSSStyle = "<style>
table {font-family: Arial, Helvetica, sans-serif; border-collapse: collapse; width: 100%;}
table td, th {border: 1px solid #ddd; padding: 8px;}
table tr:nth-child(even){background-color: #f2f2f2;}
table tr:hover {background-color: #ddd;}
table th { padding-top: 12px; padding-bottom: 12px; text-align: left; background-color: #4CAF50; color: white;}
</style>"
#Collect Site usage data
$SiteStorageData = @()
ForEach($Site in (Get-PnPTenantSite))
{
$SiteStorageData += New-Object PSObject -Property ([ordered]@{
Title = $Site.Title
URL = $Site.URL
Allocated = $Site.StorageMaximumLevel
Used = $Site.StorageUsage
Percentage = [math]::Round( ($Site.StorageUsage/$Site.StorageMaximumLevel*100),2)
})
}
#Filter Sites with usage percentage exceeding given threshold
$SitesExceeding = $SiteStorageData | Where {$_.Percentage -gt $PercentageThreshold}
If($SitesExceeding -ne $Null)
{
[string]$EmailBody = $SitesExceeding | ConvertTo-Html -Title "Crescent Inc. Storage Report" -Head $CSSStyle -PreContent "Sites Storage Report"
#Send Email
Send-PnPMail -To $EmailTo -Subject "Storage Report" -Body $EmailBody
}
Here is another post on generating site storage report: SharePoint Online: Site Collection Storage Usage Report using PowerShell
Conclusion
In conclusion, monitoring storage usage in SharePoint Online is important for ensuring that your site collections have enough storage capacity to meet your organization’s needs. Several methods are available to monitor storage usage in SharePoint Online, including the Storage Metrics page, PowerShell, and Office 365 admin center.
By regularly monitoring storage usage in SharePoint Online, you can proactively plan for additional storage needs, optimize the use of storage, and avoid running out of storage capacity. I hope this tutorial has been helpful in explaining how to monitor storage usage in SharePoint Online.
When you go over the SharePoint storage limit, you will receive an error message on the SharePoint site indicating that your storage contents have exceeded the limit. You can still upload files even after exceeding the limit, However, you can’t modify the contents or permissions. You can adjust the storage quota or purchase additional storage to resolve the issue!
The storage limit for SharePoint Online varies depending on the plan you have. For example, the storage limit for the Office 365 Business Essentials and Business Premium plans is 1 TB per organization plus 10 GB per license purchased. The storage limit for the Enterprise E1, E3, and E5 plans is 1 TB per user.
The maximum storage capacity for each site collection in SharePoint Online is 25 TB!