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