How to Get All the Containers From a Specific Azure Storage Account using PowerShell

 Introduction

In this blog, you will see how to get all the containers from a specific 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.  
  7. ## Connect to Azure Account  
  8. Connect-AzAccount   
  9.  
  10. ## Function to get all the containers  
  11. Function GetAllStorageContainer  
  12. {  
  13.     Write-Host -ForegroundColor Green "Retrieving storage container.."      
  14.     ## Get the storage account from which container has to be retrieved  
  15.     $storageAcc=Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccName      
  16.     ## Get the storage account context  
  17.     $ctx=$storageAcc.Context  
  18.     ## List all the containers  
  19.     $containers=Get-AzStorageContainer  -Context $ctx   
  20.     foreach($container in $containers)  
  21.     {  
  22.         write-host -ForegroundColor Yellow $container.Name  
  23.     }  
  24. }   
  25.   
  26. GetAllStorageContainer   
  27.  
  28. ## Disconnect from Azure Account  
  29. Disconnect-AzAccount  

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

Run the following command.

.\script.ps1 

Result:

All container names will be displayed.

Reference:

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

Summary:

In this blog, you learned how to get all the containers from a specific Azure Storage Account using PowerShell.