Calculate the size of a blob container with PowerShell
This script calculates the size of a container in Azure Blob Storage. It first displays the total number of bytes used by the blobs within the container, then displays their individual names and lengths.
This sample requires Azure PowerShell. Run Get-Module -ListAvailable Az
to find the version.
If you need to install or upgrade, see Install Azure PowerShell module.
Run the Connect-AzAccount cmdlet to connect to Azure.
If you don't have an Azure subscription, create an Azure free account before you begin.
Important
This PowerShell script provides an estimated size for the container and should not be used for billing calculations. For a script that calculates container size for billing purposes, see Calculate the size of a Blob storage container for billing purposes.
Sample script
# this script will show how to get the total size of the blobs in a container
# before running this, you need to create a storage account, create a container,
# and upload some blobs into the container
# note: this retrieves all of the blobs in the container in one command.
# if you are going to run this against a container with a lot of blobs
# (more than a couple hundred), use continuation tokens to retrieve
# the list of blobs.
# these are for the storage account to be used
$resourceGroup = "bloblisttestrg"
$storageAccountName = "contosobloblisttest"
$containerName = "listtestblobs"
# get a reference to the storage account and the context
$storageAccount = Get-AzStorageAccount `
-ResourceGroupName $resourceGroup `
-Name $storageAccountName
$ctx = $storageAccount.Context
# get a list of all of the blobs in the container
$listOfBlobs = Get-AzStorageBlob -Container $containerName -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}
# output the blobs and their sizes and the total
Write-Host "List of Blobs and their size (length)"
Write-Host " "
$listOfBlobs | select Name, Length
Write-Host " "
Write-Host "Total Length = " $length
Clean up deployment
Run the following command to remove the resource group, container, and all related resources.
Remove-AzResourceGroup -Name bloblisttestrg
Script explanation
This script uses the following commands to calculate the size of the Blob storage container. Each item in the table links to command-specific documentation.
Command | Notes |
---|---|
Get-AzStorageAccount | Gets a specified Storage account or all of the Storage accounts in a resource group or the subscription. |
Get-AzStorageBlob | Lists blobs in a container. |
Next steps
For a script that calculates container size for billing purposes, see Calculate the size of a Blob storage container for billing purposes.
For more information on the Azure PowerShell module, see Azure PowerShell documentation.
Find more PowerShell script samples in PowerShell samples for Azure Storage.
Feedback
Submit and view feedback for