Use PnP PowerShell to get sitess storage metrics.
#region DYNAMIC VARIABLES
$SPO_RootUrl = "https://xxxxxxxx.sharepoint.com"
$clientId = "xxxxx-xxxx-xxxx"
$thumbprint = "xxxxx"
$tenantId = "xxxxx"
$csvFilePath = "C:\temp\SPO_Storage.csv"
$logFilePath = "C:\temp\SPO_StorageAssement.txt"
# Define the column order as an array
$columnOrder = @(
"Url",
"Title",
"StorageUsageCurrent"
"TotalFileCount",
"TotalSize_GB",
"TotalFileStreamSize_GB"
)
#endregion
#Register-PnPAzureADApp -ApplicationName "SPOStorageApp" -Tenant "xxxxx.onmicrosoft.com" -Store CurrentUser -GraphApplicationPermissions "Sites.FullControl.All" -SharePointApplicationPermissions "Sites.FullControl.All" -Interactive
#region INITIALIZATION
$connectionsParams = @{
ClientId = $clientId
Thumbprint = $thumbprint
Tenant = $tenantId }
Connect-PnPOnline -Url $SPO_RootUrl @connectionsParams
# Start transcript logging
Start-Transcript -Path $logFilePath -Append
# Record the start time
$startTime = Get-Date
#endregion
#region FUNCTIONS
function Get-SiteInformation {
[CmdletBinding()]
param (
[system.object]$Site,
[string]$CsvFilePath,
[Hashtable]$ConnectionsParams
)
write-host "Processing $($Site.Url)"
Connect-PnPOnline -Url $Site.Url @connectionsParams
# Get Storage metrics
$pnpStorageMetrics = Get-PnPFolderStorageMetric
#region Export
# Add the site information to the exportData array (optionnel)
# Store information in a custom PowerShell object
$siteInfo = New-Object PSObject -Property @{
Url = $Site.Url
Title = $Site.Title
StorageUsageCurrent = $Site.StorageUsageCurrent
TotalFileCount = $pnpStorageMetrics.TotalFileCount
TotalSize_GB = [math]::Round($pnpStorageMetrics.TotalSize / 1GB, 2)
TotalFileStreamSize_GB = [math]::Round($pnpStorageMetrics.TotalFileStreamSize / 1GB, 2)
}
write-host "export csv $($csvFilePath)" -ForegroundColor Yellow
try {
# Export the data for the current site to the CSV file with explicit column order
$siteInfo | Select-Object $columnOrder | Export-Csv -Path $csvFilePath -Append -NoTypeInformation -Delimiter ";"
}
catch {
Write-Host "Error processing site $($Site.Url)" -ForegroundColor Red
}
finally {
}
#endregion
}
#endregion
#############################################################################
# GET ALL SP SITES
#############################################################################
$filteredSites = Get-PnPTenantSite
#############################################################################
# PROCESS SITES
#############################################################################
foreach ($site in $filteredSites)
{
Get-SiteInformation -Site $site -CsvFilePath $csvFilePath -ConnectionsParams $connectionsParams
}
#endregion
#region END
# Disconnect from SharePoint Online
Disconnect-PnPOnline
# Record the end time
$endTime = Get-Date
# Calculate the time difference
$executionTime = $endTime - $startTime
# Print the execution time in seconds
Write-Host "Script Execution Time: $($executionTime.TotalSeconds) seconds"
#endregion
# Stop transcript logging
Stop-Transcript