Skip to content

Instantly share code, notes, and snippets.

@jayankandathil
Last active January 22, 2020 16:57
Show Gist options
  • Save jayankandathil/fa8f6e0c2bf9784c06b13cfb9e57f74a to your computer and use it in GitHub Desktop.
Save jayankandathil/fa8f6e0c2bf9784c06b13cfb9e57f74a to your computer and use it in GitHub Desktop.
Slightly customized PowerShell Script from Microsoft that enumerates all blobs in an Azure blobstorage container and writes out a .csv file to local disk
# Original script from Microsoft at https://docs.microsoft.com/en-us/azure/storage/scripts/storage-blobs-container-calculate-size-powershell
# Login to Azure interactively
Login-AzAccount
# Specify the Azure subscription
Select-AzSubscription -SubscriptionName "My Subscription"
$resourceGroup = "my_resourcegroup"
$storageAccountName = "mystorageaccount"
$containerName = "mycontainer"
# Set storage context
$storageAccount = Get-AzStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccountName
$ctx = $storageAccount.Context
# Get a list of all of the blobs in the container (millions of blobs means PowerShell memory usage will reach 10s of GB)
# In that case, make sure you do this on an instance with 32 GB or more of memory
$listOfBLobs = Get-AzStorageBlob -Container $ContainerName -Context $ctx
# Zero out our total
$length = 0
# This loops through the list of blobs, retrieves the length for each blob and adds it to the total
$listOfBlobs | ForEach-Object {$length = $length + $_.Length}
Write-Host "Total Length = " $length
# Output the blobs and their sizes and the total
$listOfBlobs | select Name, Length, LastModified, IsDeleted | Export-Csv -Path C:\TEMP\blobs.csv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment