Entra / Microsoft 365 · SharePoint & OneDrive
Report spo site storage used graph
A Graph-based script to report SharePoint Online Site Storage usage data.
Connect & set up
Run these once per session. All scopes are read-only unless the script makes changes.
Connect-MgGraph -NoWelcome -Scopes Reports.Read.All, Directory.Read.All
Run it
The main script. Copy it, or download the .ps1 and run it from your console.
Clear-HostConnect-MgGraph -NoWelcome -Scopes Reports.Read.All, Directory.Read.All$ObscureFlag = $false$TempDownloadFile = "c:\temp\x.csv"# Check if the tenant has obscured real names for reports - see https://office365itpros.com/2022/09/09/graph-usage-report-tips/$DisplaySettings = Invoke-MgGraphRequest -Method Get -Uri 'https://graph.microsoft.com/V1.0/admin/reportSettings'If ($DisplaySettings['displayConcealedNames'] -eq $true) { # data is obscured, so let's reset it to allow the report to run$ObscureFlag = $trueWrite-Host "Setting tenant data concealment for reports to False" -foregroundcolor redInvoke-MgGraphRequest -Method PATCH -Uri 'https://graph.microsoft.com/V1.0/admin/reportSettings' `-Body (@{"displayConcealedNames"= $false} | ConvertTo-Json)}$CSVOutputFile = "c:\temp\SPOSiteConsumption.CSV"[array]$SiteTypesToReport = "Group", "Team Channel", "Team Site"Write-Host "Fetching SharePoint Online site data from the Graph..."# Get SharePoint files usage data - includes redirects, so we will have to remove them$URI = "https://graph.microsoft.com/v1.0/reports/getSharePointSiteUsageDetail(period='D180')"Invoke-MgGraphRequest -Uri $Uri -Method GET -OutputFilePath $TempDownloadFile[array]$Sites = Import-CSV $TempDownloadFile# Remove redirect sites. These are not really sites, but just placeholders that point to the real site, and they don't have any activity or storage usage themselves, so we can remove them from the report. Comment this out if you want to include the redirect sites in the report.$Sites = $Sites | Where-Object {$_.Template -ne "REDIRECTSITE#0"}$TotalSPOStorageUsed = [Math]::Round(($Sites."Storage Used (Byte)" | Measure-Object -Sum).Sum /1GB,2)$Report = [System.Collections.Generic.List[Object]]::new()ForEach ($Site in $Sites) {$DoNotProcessSite = $FalseIf ([string]::IsNullOrEmpty($Site."Last Activity Date")) {$LastActiveDate = "No Activity"} Else {$LastActiveDate = Get-Date ($Site."Last Activity Date") -Format dd-MMM-yyyy}# Check for redirect sites returned by the Graph so we don't process themIf (($Site."Owner Display Name" -eq "System Account") -and ([string]::IsNullOrEmpty($Site."Owner Principal Name"))) {$DoNotProcessSite = $True}# Check for the fundamental site because we don't want to process it eitherIf ($Site."Root Web Template" -notin $SiteTypesToReport) {$DoNotProcessSite = $True}If ($DoNotProcessSite -eq $False) {$UsedGB = [Math]::Round($Site."Storage Used (Byte)"/1GB,2)$PercentTenant = ([Math]::Round($Site.StorageUsageCurrent/1024,4)/$TotalSPOStorageUsed).tostring("P")$ReportLine = [PSCustomObject]@{URL = $Site."Site URL"Owner = $Site."Owner Display Name"OwnerUPN = $Site."Owner Principal Name"Files = $Site."File Count"ActiveFiles = $Site."Active File Count"LastActiveDate = $LastActiveDateTemplate = $Site."Root Web Template"QuotaGB = [Math]::Round($Site."Storage Allocated (Byte)"/1GB,0)UsedGB = $UsedGBPercentUsed = ([Math]::Round(($Site."Storage Used (Byte)"/$Site."Storage Allocated (Byte)"),4).ToString("P"))PercentTenant = $PercentTenant}$Report.Add($ReportLine) }}# Switch the tenant report obscure data setting back if necessaryIf ($ObscureFlag -eq $True) {Write-Host "Resetting tenant data concealment for reports to True" -foregroundcolor redInvoke-MgGraphRequest -Method PATCH -Uri 'https://graph.microsoft.com/V1.0/admin/reportSettings' `-Body (@{"displayConcealedNames"= $true} | ConvertTo-Json)}$Report | Export-CSV -NoTypeInformation $CSVOutputFile$Report | Sort-Object {$_.UsedGB -as [decimal]}, url -Descending | Out-GridViewWrite-Host ("{0} sites processed. Current SharePoint Online storage consumption is {1} GB. Report file available in {2}" -f $Report.count, $TotalSPOStorageUsed, $CSVOutputReport)
Attribution
Author
Office365itpros