How to Download Blob Contents from an Azure Storage Account using PowerShell

Introduction 

In this blog, you will see how to download blob contents from an 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. $downloadPath=".\Download"  
  7. $downloadLocation="Download"  
  8.  
  9. ## Connect to Azure Account  
  10. Connect-AzAccount   
  11.  
  12. ## Function to dlownload all blob contents  
  13. Function DownloadBlobContents  
  14. {  
  15.     Write-Host -ForegroundColor Green "Download blob contents from storage container.."    
  16.     ## Get the storage account  
  17.     $storageAcc=Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccName     
  18.     ## Get the storage account context  
  19.     $ctx=$storageAcc.Context  
  20.     ## Get all the containers  
  21.     $containers=Get-AzStorageContainer  -Context $ctx   
  22.     ## Loop through the containers  
  23.     foreach($container in $containers)  
  24.     {          
  25.         ## check if folder exists  
  26.         $folderPath=$downloadPath+"\"+$container.Name  
  27.         $destination=$downloadLocation+"\"+$container.Name  
  28.         $folderExists=Test-Path -Path $folderPath  
  29.         if($folderExists)  
  30.         {  
  31.             Write-Host -ForegroundColor Magenta $container.Name "- folder exists"  
  32.             ## Get the blob contents from the container  
  33.             $blobContents=Get-AzStorageBlob -Container $container.Name  -Context $ctx  
  34.             foreach($blobContent in $blobContents)  
  35.             {  
  36.                 ## Download the blob content  
  37.                 Get-AzStorageBlobContent -Container $container.Name  -Context $ctx -Blob $blobContent.Name -Destination $destination -Force  
  38.             }  
  39.         }  
  40.         else  
  41.         {        
  42.             Write-Host -ForegroundColor Magenta $container.Name "- folder does not exist"  
  43.             ## Create the new folder  
  44.             New-Item -ItemType Directory -Path $folderPath  
  45.             ## Get the blob contents from the container  
  46.             $blobContents=Get-AzStorageBlob -Container $container.Name  -Context $ctx  
  47.             foreach($blobContent in $blobContents)  
  48.             {  
  49.                 ## Download the blob content  
  50.                 Get-AzStorageBlobContent -Container $container.Name  -Context $ctx -Blob $blobContent.Name -Destination $destination -Force  
  51.             }  
  52.         }  
  53.     }     
  54. }   
  55.   
  56. DownloadBlobContents  
  57.  
  58. ## Disconnect from Azure Account  
  59. 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 the blob contents from the Azure Storage account have downloaded successfully.

Note: Folders will be created with the same name as the container name. If the folder exists, documents are downloaded in the respective folder. 
 
 
 
 

Reference:

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

Summary:

In this blog, you saw how to download blob contents from an Azure Storage Account using PowerShell.