How to obtain total storage used by all subscriptions and subsequent storage accounts in GB or TB with a script?
You can use the below PowerShell script to get the used capacity
of all Azure Storage accounts in GB.
Script:
Connect-AzAccount -Tenant 'xxxx-xxxx-xxxx-xxxx'
$subs = Get-AzSubscription
$subs | ForEach-Object {
Set-AzContext -SubscriptionId $_.Id
Write-Host "`nProcessing Subscription: $($_.Name)" -ForegroundColor Cyan
$resourceGroups = Get-AzResourceGroup
$results = @()
foreach ($rg in $resourceGroups) {
$rgName = $rg.ResourceGroupName
Write-Host "`nChecking resource group: $rgName" -ForegroundColor Yellow
$storageAccounts = Get-AzStorageAccount -ResourceGroupName $rgName
foreach ($sa in $storageAccounts) {
$storageName = $sa.StorageAccountName
$saId = $sa.Id
$metric = Get-AzMetric -ResourceId $saId -MetricName "UsedCapacity" -WarningAction SilentlyContinue
if ($metric -and $metric.Data.Count -gt 0) {
$averageBytes = ($metric.Data | Where-Object { $_.Average -ne $null } | Select-Object -Last 1).Average
$capacityInGB = [math]::Round($averageBytes / 1GB, 2)
} else {
$capacityInGB = 0
}
$results += [PSCustomObject]@{
SubscriptionName = $_.Name
SubscriptionId = $_.Id
ResourceGroupName = $rgName
StorageAccountName = $storageName
UsedCapacityInGB = $capacityInGB
}
}
}
# Display results
$results | Format-Table SubscriptionName, SubscriptionId, ResourceGroupName, StorageAccountName, UsedCapacityInGB -AutoSize
}
Output:
Hope this answer helps! please let us know if you have any further queries. I’m happy to assist you further.
Please do not forget to "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.