How to obtain total storage used by all subscriptions and subsequent storage accounts in GB or TB with a script

Advay Thombare 25 Reputation points
2025-04-18T19:12:42.8133333+00:00

Hi Everyone,

I am working on obtaining the total amount of storage space as you can see below, I can get that inside the Monitor>Insights>Storage Accounts, but I want to find a way to obtain that same value using a script either PowerShell or Bash, been working on some CLI commands but none of them are working, has somebody done this or can you point me in the right direction please.User's image

Thank You for your time

Best Regards.

Advay Thombare

Azure Storage
Azure Storage
Globally unique resources that provide access to data management services and serve as the parent namespace for the services.
3,532 questions
{count} votes

Accepted answer
  1. Venkatesan S 2,820 Reputation points Microsoft External Staff Moderator
    2025-04-22T11:55:07.38+00:00

    Hi @Advay Thombare

    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: enter image description here
    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.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Keshavulu Dasari 4,840 Reputation points Microsoft External Staff Moderator
    2025-04-18T19:40:04.13+00:00

    Hi Advay Thombare,

    You can use either PowerShell or Bash to obtain the total storage used by all subscriptions and storage accounts. examples for both:

    PowerShell Script

    You can use the Get-AzMetric and Get-AzStorageAccountUsage cmdlets to retrieve the storage metrics:

    # Login to your Azure account
    Connect-AzAccount
    
    # Set the context to the subscription that contains your storage account
    Set-AzContext -SubscriptionId <SubscriptionId>
    
    # Retrieve the total allocated size of the storage account
    $storageAccountName = "<StorageAccountName>"
    $resourceId = "/subscriptions/<SubscriptionId>/resourceGroups/<ResourceGroupName>/providers/Microsoft.Storage/storageAccounts/$storageAccountName"
    $metric = Get-AzMetric -ResourceId $resourceId -TimeGrain PT1H -MetricName BlobCapacity -AggregationType Total -StartDate (Get-Date).AddDays(-1) -EndTime (Get-Date)
    $totalSize = $metric.Data | Select-Object -ExpandProperty Total | Select-Object -First 1
    
    # Retrieve the consumed size for each storage type
    $usage = Get-AzStorageAccountUsage -ResourceGroupName <ResourceGroupName> -Name $storageAccountName
    $blobSize = $usage | Where-Object {$_.Name.Value -eq "BlobCapacity"} | Select-Object -ExpandProperty CurrentValue
    $tableSize = $usage | Where-Object {$_.Name.Value -eq "TableCapacity"} | Select-Object -ExpandProperty CurrentValue
    $queueSize = $usage | Where-Object {$_.Name.Value -eq "QueueCapacity"} | Select-Object -ExpandProperty CurrentValue
    $fileSize = $usage | Where-Object {$_.Name.Value -eq "FileCapacity"} | Select-Object -ExpandProperty CurrentValue
    
    # Print the results
    Write-Host "Total allocated size: $totalSize bytes"
    Write-Host "Blob consumed size: $blobSize bytes"
    Write-Host "Table consumed size: $tableSize bytes"
    Write-Host "Queue consumed size: $queueSize bytes"
    Write-Host "File consumed size: $fileSize bytes"
    
    
    

    Bash Script

    You can use the az rest command to invoke the Metrics API and parse the response:

    # Set variables
    subscriptionId="<SubscriptionId>"
    resourceGroupName="<ResourceGroupName>"
    storageAccountName="<StorageAccountName>"
    
    # Get the metrics
    response=$(az rest --uri "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Storage/storageAccounts/$storageAccountName/providers/Microsoft.Insights/metrics?api-version=2018-01-01&metricnames=UsedCapacity&aggregation=Average")
    
    # Parse the response to get the used capacity
    usedCapacity=$(echo $response | jq '.value[0].timeseries[0].data[0].average')
    
    # Print the result
    echo "Used capacity: $usedCapacity bytes"
    
    

    For more information:

    https://techcommunity.microsoft.com/blog/azurepaasblog/calculate-the-sizecapacity-of-storage-account-and-it-services-blobtable/1064046

    https://stackoverflow.com/questions/69732270/how-to-get-the-size-of-an-azure-storage-account-in-azure-cli-or-bash-script

    If you have any other questions or are still running into more issues, let me know in the "comments" and I would be glad to assist you.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.