Microsoft OneDrive provides cloud storage for users in an organization. However, to maintain control over storage consumption and avoid excessive usage, IT administrators must enforce storage limits. Using PnP PowerShell, you can efficiently manage and enforce OneDrive storage quotas for users in your Microsoft 365 environment.
This guide will provide a step-by-step approach to setting, updating, and enforcing OneDrive storage limits using PnP PowerShell.
Step 1: Install and Update PnP PowerShell
Ensure that PnP PowerShell is installed on your system. Open PowerShell as an Administrator and run the following command:
Install-Module -Name PnP.PowerShell -Force -AllowClobber
To update an existing version:
Update-Module -Name PnP.PowerShell
You can verify the installation by running:
Get-Module -Name PnP.PowerShell -ListAvailable
Step 2: Connect to SharePoint Online Admin Center
To enforce OneDrive storage limits, you must connect to your SharePoint Online Admin Center, as OneDrive is managed via SharePoint settings.
$adminUrl = "https://yourtenant-admin.sharepoint.com"
Connect-PnPOnline -Url $adminUrl -Scopes "Sites.FullControl.All" -Interactive
If using App-based authentication, use:
$clientId = "your-client-id"
$tenantId = "your-tenant-id"
$clientSecret = "your-client-secret"
Connect-PnPOnline -Url $adminUrl -ClientId $clientId -ClientSecret $clientSecret -Tenant $tenantId
This will authenticate your session and allow access to OneDrive settings.
Step 3: Check Current OneDrive Storage Quota
Before enforcing storage limits, check the current storage quota for a user’s OneDrive:
$userEmail = "user@yourdomain.com"
$oneDriveSiteUrl = "https://yourtenant-my.sharepoint.com/personal/" + ($userEmail -replace "@", "_") + "/"
Get-PnPTenantSite -Url $oneDriveSiteUrl | Select-Object Url, StorageQuota, StorageQuotaWarningLevel
This command retrieves:
- StorageQuota – The maximum storage limit in MB.
- StorageQuotaWarningLevel – The warning level when a user approaches the limit.
Step 4: Set OneDrive Storage Quota for a User
To set a specific storage limit (e.g., 100 GB), run:
$userEmail = "user@yourdomain.com"
$oneDriveSiteUrl = "https://yourtenant-my.sharepoint.com/personal/" + ($userEmail -replace "@", "_") + "/"
$storageQuota = 102400 # 100 GB in MB
$warningLevel = 92160 # 90 GB in MB
Set-PnPTenantSite -Url $oneDriveSiteUrl -StorageQuota $storageQuota -StorageQuotaWarningLevel $warningLevel
This ensures that:
- The user’s total OneDrive storage is limited to 100 GB.
- A warning is triggered when they exceed 90 GB.
Step 5: Apply Storage Quota to All OneDrive Users
To apply a universal storage limit (e.g., 50 GB) to all OneDrive users:
$storageQuota = 51200 # 50 GB in MB
$warningLevel = 46080 # 45 GB in MB
$oneDriveSites = Get-PnPTenantSite -IncludeOneDriveSites | Where-Object { $_.Url -like "*-my.sharepoint.com/personal/*" }
foreach ($site in $oneDriveSites) {
Set-PnPTenantSite -Url $site.Url -StorageQuota $storageQuota -StorageQuotaWarningLevel $warningLevel
Write-Host "Updated storage for: $($site.Url)"
}
This script:
- Retrieves all OneDrive sites.
- Sets a storage limit of 50 GB.
- Applies a warning level at 45 GB.
Step 6: Automate Enforcement of Storage Quotas
To enforce storage limits automatically, schedule a PowerShell script.
- Create a PowerShell script (
Enforce-OneDriveQuota.ps1
)
# Connect to SharePoint Online Admin Center
$adminUrl = "https://yourtenant-admin.sharepoint.com"
Connect-PnPOnline -Url $adminUrl -Scopes "Sites.FullControl.All" -Interactive
# Define storage quota settings
$storageQuota = 102400 # 100 GB in MB
$warningLevel = 92160 # 90 GB in MB
# Get all OneDrive sites
$oneDriveSites = Get-PnPTenantSite -IncludeOneDriveSites | Where-Object { $_.Url -like "*-my.sharepoint.com/personal/*" }
# Apply storage limits
foreach ($site in $oneDriveSites) {
Set-PnPTenantSite -Url $site.Url -StorageQuota $storageQuota -StorageQuotaWarningLevel $warningLevel
Write-Host "Updated storage limit for: $($site.Url)"
}
Write-Host "OneDrive storage enforcement completed."
- Schedule the Script to Run Daily
- Open Task Scheduler.
- Click Create Basic Task.
- Name it “Enforce OneDrive Storage Limits”.
- Set it to run Daily.
- Choose Start a Program.
- In Program/Script, enter: plaintextCopyEdit
powershell.exe
- In Arguments, enter: plaintextCopyEdit
-File "C:\Scripts\Enforce-OneDriveQuota.ps1"
- Click Finish.
Now, the script will run daily and enforce storage limits automatically.
Step 7: Monitor OneDrive Storage Usage
To identify users exceeding their OneDrive quota:
$overLimitUsers = Get-PnPTenantSite -IncludeOneDriveSites | Where-Object { $_.StorageUsageCurrent -gt $_.StorageQuota }
$overLimitUsers | Select-Object Url, StorageUsageCurrent, StorageQuota | Format-Table -AutoSize
To export the report to CSV:
$overLimitUsers | Select-Object Url, StorageUsageCurrent, StorageQuota | Export-Csv -Path "C:\Reports\OneDrive_OverLimit.csv" -NoTypeInformation
Write-Host "Report saved to C:\Reports\OneDrive_OverLimit.csv"