Azure Tip: Use PowerShell to check all blob spaced used in a Storage Account

Just recently, I had the need to be able to know the exact volume of all blob container data, within a specific Azure Storage Account.

This was part of a migration project, which in this case meant that I needed to report that data amount multiple times per day. Data was constantly being copied to and deleted from that Storage account, and the same applies to Blob containers being created, filled with data and deleted afterwards. So my only constant was the Storage Account, and I needed to know, every 2 hours, what was the volume of blob container data in that account.

After a quick of research I found this outstanding Microsoft article on how to leverage the Azure PowerShell module (yes, PowerShell to save the day again!!) to calculate the size of a Blob Storage Container.

The only limitation with the script in the article above was that it’s calculating the size of a single blob container, and I needed the combined size of all blob containers in my Storage Account.

So I had to adapt that script to my scenario, and I turned it into the following script:

# Connect to Azure
Connect-AzureRmAccount

# Static Values for Resource Group and Storage Account Names
$resourceGroup = "ChangeToYourResourceGroupName"
$storageAccountName = "changetoyourstorageaccountname"

# Get a reference to the storage account and the context
$storageAccount = Get-AzureRmStorageAccount `
-ResourceGroupName $resourceGroup `
-Name $storageAccountName
$ctx = $storageAccount.Context

# Get All Blob Containers
$AllContainers = Get-AzureStorageContainer -Context $ctx
$AllContainersCount = $AllContainers.Count
Write-Host "We found '$($AllContainersCount)' containers. Processing size for each one"

# Zero counters
$TotalLength = 0
$TotalContainers = 0

# Loop to go over each container and calculate size
Foreach ($Container in $AllContainers){
$TotalContainers = $TotalContainers + 1
Write-Host "Processing Container '$($TotalContainers)'/'$($AllContainersCount)'"
$listOfBLobs = Get-AzureStorageBlob -Container $Container.Name -Context $ctx

# zero out our total
$length = 0

# this loops through the list of blobs and retrieves the length for each blob and adds it to the total
$listOfBlobs | ForEach-Object {$length = $length + $_.Length}
$TotalLength = $TotalLength + $length
}
# end container loop

#Convert length to GB
$TotalLengthGB = $TotalLength /1024 /1024 /1024

# Result output
Write-Host "Total Length = " $TotallengthGB "GB"

 

The script above will provide you an output into the console of the total volume, in GB, that you have on a specific storage account.

To execute the script, follow the steps below:

  • Copy the entire code above to a notepad
  • Change the values of line 2 and 3, to the correct names of your Azure Resource group and your Azure Storage Account
  • Save the file as .ps1
  • Open a PowerShell window and execute the “script.ps1” file you just saved (see screenshot below)
  • Authenticate with your Azure username and password, when prompted

ScriptAllBlobs1

Execute the script as shown above.

AzureAuth

When prompted, authenticate.

endresult

And this is how the end result should look like.

Before I end this blog post I’d just like to point out that this script was written in a very simplistic way, and to address an urgent need that I had. With a couple more hours of work, you can make this script even easier to use and add all sorts of different features to it, such as:

  • Error handling
  • remove the hard coded values and list for selection all available storage accounts and resource groups
  • change the output format (i.e to CSV) and list sizes per blob container
  • allow you to select between multiple Azure subscriptions under the same account

The above are just some ideas on how to improve the script. I haven’t done it because I had no need for it, but by all means please let me know if you want/need an improved version. This one works just fine, if all you want is the total volume of blob data in a specific storage account.

Happy New Year!!!

11 thoughts on “Azure Tip: Use PowerShell to check all blob spaced used in a Storage Account

  1. San August 13, 2019 / 8:48 pm

    Hi Antonio, This is a great script, worked like a charm. The customer has a requirement to list each of the storage accounts under a subscription and their usage?
    Would it be possible to expand this script to include each storage account and its usage,

    • Antonio Vargas November 12, 2019 / 12:39 pm

      San, sorry I missed your comment. Is this still a requirement to you?

  2. Antonio Vargas November 12, 2019 / 12:38 pm

    San, sorry I missed your comment. Is this still a requirement to you?

  3. Jason Powell November 12, 2019 / 6:35 pm

    Any plans to convert this into the newest Powershell for Azure : Az?

    • Antonio Vargas November 12, 2019 / 12:37 pm

      Hi Jason. I will work on it as soon as I can. Thank you for the feedback.

  4. Geoff A December 2, 2019 / 8:21 am

    Here is how you can get ALL storage account info for a specific Azure subscription.

    # Connect to Azure
    Connect-AzureRmAccount -SubscriptionName ‘ENTER YOUR SUBSCRIPTION NAME HERE’
    Clear-Host
    $array = Get-AzureRmStorageAccount

    foreach ($a in $array) {

    $myresourceGroup = $a.ResourceGroupName
    $mystorageAccountName = $a.storageAccountName

    # Static Values for Resource Group and Storage Account Names
    $resourceGroup = $myresourceGroup
    $storageAccountName = $mystorageAccountName

    write-host $myresourceGroup ” ” $mystorageAccountName

    # Get a reference to the storage account and the context
    $storageAccount = Get-AzureRmStorageAccount `
    -ResourceGroupName $resourceGroup `
    -Name $storageAccountName
    $ctx = $storageAccount.Context

    # Get All Blob Containers
    $AllContainers = Get-AzureStorageContainer -Context $ctx
    $AllContainersCount = $AllContainers.Count
    Write-Host “We found ‘$($AllContainersCount)’ containers in $mystorageAccountName. Processing size for each one”

    # Zero counters
    $TotalLength = 0
    $TotalContainers = 0

    # Loop to go over each container and calculate size
    Foreach ($Container in $AllContainers){
    $TotalContainers = $TotalContainers + 1
    #Write-Host “Processing Container ‘$($TotalContainers)’/’$($AllContainersCount)'”
    $listOfBLobs = Get-AzureStorageBlob -Container $Container.Name -Context $ctx

    # zero out our total
    $length = 0

    # this loops through the list of blobs and retrieves the length for each blob and adds it to the total
    $listOfBlobs | ForEach-Object {$length = $length + $_.Length}
    $TotalLength = $TotalLength + $length
    }
    # end container loop

    #Convert length to GB
    $TotalLengthGB = $TotalLength /1024 /1024 /1024

    # Result output
    Write-Host “Total Length = ” $TotallengthGB “GB”

    }

  5. badbob July 2, 2020 / 9:37 pm

    Does this just get the provisioned size of the blobs in the container and not the actual size used, which may be much less? For example, if you create a new vhd blob of size 127GB but never use it, the provisioned size would be 127GB but the actual size may be close to 0.

    • Antonio Vargas July 2, 2020 / 9:59 pm

      Used size, since this is blob storage. Is that not what you’re seeing?

    • Antonio Vargas July 2, 2020 / 10:01 pm

      The VHD in the blob is using the total space it has, so if you create a 127GB VHD this script will show the 127gb usage in the blob. You would have to go a different route to understand the usage of the vhd

  6. $@chin August 12, 2021 / 4:15 pm

    Hi, Having multiple file in one container. So how to check the size of each file (in mb or GB)

    • Antonio Vargas September 21, 2021 / 10:14 am

      Script would have to be customized. Let me know if you need a customized version. Contact via blog

Leave a comment