Finding Storage Nearing Exceeding Quota with PowerShell
Monitoring Storage Quotas with PowerShell
Managing storage quotas in Microsoft 365 is critical to ensure uninterrupted service and optimal performance. This blog post demonstrates how to use a PowerShell script to monitor storage usage across SharePoint Online sites and identify sites nearing or exceeding their storage quotas.
Script Overview
The script connects to the SharePoint Online admin center, retrieves site storage details, calculates the percentage of storage used, and generates a report for sites exceeding 80% of their allocated storage quota. The report is exported as a CSV file for further analysis.
PowerShell Script
$AdminCenterURL="https://contoso-admin.sharepoint.com/"# Connect to SharePoint Online admin center
Connect-PnPOnline -Url $AdminCenterURL
$dateTime = (Get-Date).toString("dd-MM-yyyy")
$invocation = (Get-Variable MyInvocation).Value
$directorypath = Split-Path $invocation.MyCommand.Path
$fileName = "SiteStorageReport-" + $dateTime + ".csv"
$OutPutView = $directorypath + "\Logs\"+ $fileName
# Array to Hold Result - PSObjects
$siteCollection = @()
#
$m365Sites = Get-PnPTenantSite -Detailed -IncludeOneDriveSites | Where-Object { $_.Template -ne 'RedirectSite#0' } #($_.Url -like '*-my.sharepoint.com/personal/*') -and
#$m365Groups = Get-PnPMicrosoft365Group | where-object {$_.DisplayName -eq "Dev Mars"}
$m365Sites | ForEach-Object {
$PercentUsed=[Math]::Round(($_.StorageUsageCurrent/$_.StorageQuota *100),3)
if( $PercentUsed -gt 80){
$oneDrive = [PSCustomObject]@{
SiteName = $_.Title
LastContentModifiedDate=$_.LastContentModifiedDate
Template=$_.Template
SensitivityLabel=$_.SensitivityLabel
"Storage Maximum Level"=$_.StorageMaximumLevel
StorageUsageCurrentMB=$_.StorageUsageCurrent
StorageQuota=$_.StorageQuota
"StorageQuotaWarning Level"=$_.StorageQuotaWarningLevel
SharingCapability=$_.SharingCapability
PercentUsed=$PercentUsed
}
$oneDriveCollection+=$oneDrive
}
}
# Export the result array to CSV file
$siteCollection | sort-object "Group Name" |Export-CSV $OutPutView -Force -NoTypeInformation
# Disconnect SharePoint online connection
Disconnect-PnPOnline
How It Works
- Connect to SharePoint Online: The script uses
Connect-PnPOnline
to authenticate and connect to the SharePoint Online admin center. - Retrieve Site Data: It fetches all site collections, excluding redirect sites, using
Get-PnPTenantSite
. - Calculate Storage Usage: For each site, the script calculates the percentage of storage used.
- Filter Sites: Sites exceeding 80% of their storage quota are added to the results.
- Export Report: The results are exported to a timestamped CSV file for easy review.
Example Output
The script generates a CSV file with the following columns:
- SiteName: The name of the site.
- LastContentModifiedDate: The date when the site content was last modified.
- Template: The site template type.
- SensitivityLabel: The sensitivity label applied to the site.
- Storage Maximum Level: The maximum storage level for the site.
- StorageUsageCurrentMB: The current storage usage in megabytes.
- StorageQuota: The total storage quota allocated to the site.
- StorageQuotaWarning Level: The warning level for storage usage.
- SharingCapability: The sharing settings for the site.
- PercentUsed: The percentage of storage used.
Benefits of the Script
- Proactive Monitoring: Identify sites nearing their storage limits before they exceed quotas.
- Automation: Save time by automating the process of retrieving and analyzing storage data.
- Customizable: Easily modify the script to include additional filters or data points.
Notes
- Replace
https://contoso-admin.sharepoint.com/
with your SharePoint Online admin center URL. - Ensure you have the necessary permissions to run the script and access site data.
- Install the PnP PowerShell module before running the script.
By using this PowerShell script, you can efficiently monitor storage usage across your SharePoint Online environment and take proactive measures to manage storage quotas effectively.