Back to script library
Entra / Microsoft 365 · SharePoint & OneDrive

Report OneDrive usage

Example of generating a report about OneDrive for Business storage consumption using Microsoft 365 usage reports.

Connect & set up

Run these once per session. All scopes are read-only unless the script makes changes.

Connect-MgGraph -NoWelcome -Scopes User.Read.All, Reports.Read.All, ReportSettings.ReadWrite.All

Run it

The main script. Copy it, or download the .ps1 and run it from your console.

Connect-MgGraph -NoWelcome -Scopes User.Read.All, Reports.Read.All, ReportSettings.ReadWrite.All
$ObscureFlag = $false
$CSVOutputFile = "c:\temp\OneDriveSiteConsumption.CSV"
$TempExportFile = "c:\temp\TempExportFile.CSV"
If (Get-Item $TempExportFile -ErrorAction SilentlyContinue) {
Remove-Item $TempExportFile
}
# Check if the tenant has obscured real names for reports - see https://office365itpros.com/2022/09/09/graph-usage-report-tips/
If ((Get-MgAdminReportSetting).DisplayConcealedNames -eq $true) {
$Parameters = @{ displayConcealedNames = $False }
Write-Host "Unhiding obscured report data for the script to run..."
Update-MgAdminReportSetting -BodyParameter $Parameters
$ObscureFlag = $true
}
# Get user account information and load it into a hash table so that we can use it along with the OneDrive info
Write-Host "Finding user account information..."
[array]$Users = Get-MgUser -All -Filter "assignedLicenses/`$count ne 0 and userType eq 'Member'" `
-ConsistencyLevel Eventual -CountVariable UserCount -Sort 'displayName' `
-Property Id, displayName, userPrincipalName, city, country, department, jobTitle, officeLocation
$UserHash = @{}
ForEach ($User in $Users) {
$UserHash.Add($User.userPrincipalName, $User)
}
# Get a list of OneDrive for Business sites in the tenant sorted by the biggest consumer of quota
Write-Host "Finding OneDrive sites..."
# This could also be done with Get-MgReportOneDriveUsageAccountDetail -Period D7 -Outfile $TempExportFile
$Uri = "https://graph.microsoft.com/v1.0/reports/getOneDriveUsageAccountDetail(period='D7')"
Invoke-MgGraphRequest -Uri $Uri -Method GET -OutputFilePath $TempExportFile
[array]$ODFBSites = Import-CSV $TempExportFile | Sort-Object 'User display name'
If (!($ODFBSites)) {
Write-Host "No OneDrive sites found (surprisingly...)" ;
break
}
# Calculate total storage used by OneDrive for Business accounts
$TotalODFBGBUsed = [Math]::Round(($ODFBSites.'Storage Used (Byte)' | Measure-Object -Sum).Sum /1GB,2)
# Create list to store report data
$Report = [System.Collections.Generic.List[Object]]::new()
# Store information for each OneDrive site
ForEach ($Site in $ODFBSites) {
[array]$UserData = $UserHash[$Site.'Owner Principal name']
$ReportLine = [PSCustomObject]@{
Owner = $Site.'Owner display name'
UPN = $Site.'Owner Principal name'
City = $UserData.city
Country = $UserData.Country
Department = $UserData.Department
'Job Title' = $UserData.Jobtitle
QuotaGB = [Math]::Round($Site.'Storage Allocated (Byte)'/1GB,2)
UsedGB = [Math]::Round($Site.'Storage Used (Byte)'/1GB,4)
PercentUsed = [Math]::Round(($Site.'Storage Used (Byte)'/$Site.'Storage Allocated (Byte)' * 100),4)
}
$Report.Add($ReportLine)
}
$Report | Export-CSV -NoTypeInformation $CSVOutputFile
# You don't have to do this, but it's useful to view the data via Out-GridView
$Report | Sort-Object UsedGB -Descending | Out-GridView
Write-Host ("Current OneDrive for Business storage consumption is {0} GB. Report is in {1}" -f $TotalODFBGBUsed, $CSVOutputFile)
# Switch the tenant report obscure data setting back if necessary
If ($ObscureFlag -eq $true) {
Write-Host "Resetting tenant data concealment for reports to True" -foregroundcolor red
$Parameters = @{ displayConcealedNames = $True }
Update-MgAdminReportSetting -BodyParameter $Parameters
}
Attribution