How to Get All the Blobs from an Azure Storage Account using PowerShell

Introduction 

In this blog, you will see how to get all the blobs from a specific container in Azure Storage Account using PowerShell.

Prerequisites

Install Azure PowerShell Module to run the script.

PowerShell Script

Open Notepad and paste the following script. Save the file as script.ps1.

  1. ################# Azure Blob Storage - PowerShell ####################  
  2.  
  3. ## Input Parameters  
  4. $resourceGroupName="azpractice"  
  5. $storageAccName="azstorageacc1122020"  
  6. $containerName="container002"  
  7.  
  8. ## Connect to Azure Account  
  9. Connect-AzAccount   
  10.  
  11. ## Function to get all the blobs  
  12. Function GetAllBlobs  
  13. {  
  14.     Write-Host -ForegroundColor Green "Retrieving all blobs from storage container.."    
  15.     ## Get the storage account   
  16.     $storageAcc=Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccName     
  17.     ## Get the storage account context  
  18.     $ctx=$storageAcc.Context  
  19.     ## Get all the containers  
  20.     $containers=Get-AzStorageContainer  -Context $ctx         
  21.     ## Get all the blobs  
  22.     $blobs=Get-AzStorageBlob -Container $containerName  -Context $ctx  
  23.     ## Loop through all the blobs  
  24.     foreach($blob in $blobs)  
  25.     {  
  26.         write-host -Foregroundcolor Yellow $blob.Name  
  27.     }  
  28. }  
  29.   
  30. GetAllBlobs   
  31.  
  32. ## Disconnect from Azure Account  
  33. Disconnect-AzAccount  

Open the Windows PowerShell window and navigate to the location where the script file was saved.

Run the following command.

.\script.ps1

Reference

https://docs.microsoft.com/en-us/powershell/module/az.storage/get-AzStorageblob?view=azps-3.3.0

Summary

In this blog, you saw how to get all the blobs from an Azure Storage Account using PowerShell.