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 Accounts
Azure Storage Accounts
Globally unique resources that provide access to data management services and serve as the parent namespace for the services.
3,471 questions
{count} votes

Accepted answer
  1. Venkatesan S 1,625 Reputation points Microsoft External Staff
    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.


0 additional answers

Sort by: Most helpful

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.