How To Download Azure BLOB Storage Using Azure PowerShell

In this article, we are going to see the steps required to download Azure BLOB Storage contents from the container using PowerShell script. You can access the Azure resources using Azure PowerShell. Azure PowerShell contains sets of modules that provide multiple cmdlets to manage Azure with Windows PowerShell. You can build automation scripts with Azure resources.

Prerequisite

Before you begin to utilize PowerShell to oversee the Azure PowerShell, ensure that the Azure PowerShell has been installed. If not installed, here is an article How to install Azure PowerShell module. You need to do this only once for each computer from which you are running Azure PowerShell commands.

Step 1

Connect to Azure Portal using Connect-AzureRmAccount cmdlet.

Connect-AzureRmAccount

How To Download Azure BLOB Storage Using Azure PowerShell

Step 2

We need to get the Azure Storage context to access the storage content from the Azure portal. You can get the context using New-AzureStorageContext cmdlets. The required parameters are,

  • ConnectionString - Specifies a connection string for the Azure Storage context.

You can get the connection string from the Azure portal. Open your Azure portal and select Storage account from the side menu. It will look like the snapshot below.

How To Download Azure BLOB Storage Using Azure PowerShell

Copy the connection string. It will look like the below snapshot.

How To Download Azure BLOB Storage Using Azure PowerShell

Assign connection string in a variable and pass the value to the -ConnectionString parameter,

  1. $connection_string = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'  
  2. $storage_account = New-AzureStorageContext -ConnectionString $connection_string  
How To Download Azure BLOB Storage Using Azure PowerShell

Step 3

To get BLOBS in a container, use Get-AzureStorageBlob cmdlet. The required parameters are,

  • Container - Specify the name of the container
  • Context - Specify the context to access the storage BLOBS
    1. $container_name = 'hubflycontainer1'  
    2. $blobs = Get-AzureStorageBlob -Container $container_name -Context $storage_account  
How To Download Azure BLOB Storage Using Azure PowerShell

Step 4

Create a directory using the New-Item cmdlet. The required parameters are,

  • ItemType - Specify the item type as directory
  • Path - Specify the Path where the directory should be created
    1. $destination_path = 'E:\DEMO'  
    2. New-Item -ItemType Directory -Force -Path $destination_path  
How To Download Azure BLOB Storage Using Azure PowerShell

Step 5

You can download the Azure Storage BLOBS using Get-AzureStorageBlobContent cmdlets. The required parameters are,

  • Container - Specify the name of the container
  • Blob - Specify the blob Name
  • Destination - Specify the Path where the blob's contents should download
  • Context - Specify the context to access the storage BLOBS
    1. $blobs = Get-AzureStorageBlob -Container $container_name -Context $storage_account  
    2. foreach ($blob in $blobs)  
    3. {  
    4.    New-Item -ItemType Directory -Force -Path $destination_path  
    5.    Get-AzureStorageBlobContent -Container $container_name -Blob $blob.Name -Destination $destination_path -Context $storage_account  
    6. }  

Now, you can check the destination path. You will see the BLOB content being downloaded.

Final code

  1. Connect - AzureRmAccount  
  2. $container_name = 'hubflycontainer'  
  3. $destination_path = 'E:\CDN-BACKUPS\CDN'  
  4. $connection_string = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'  
  5. $storage_account = New - AzureStorageContext - ConnectionString $connection_string  
  6. $blobs = Get - AzureStorageBlob - Container $container_name - Context $storage_account  
  7. foreach($blob in $blobs) {  
  8.     New - Item - ItemType Directory - Force - Path $destination_path  
  9.     Get - AzureStorageBlobContent `  
  10. -Container $container_name -Blob $blob.Name -Destination $destination_path ` - Context $storage_account  
  11. }  
I hope you have now learned how to download storage blobs using Azure PowerShell programmatically. Feel free to fill up the comment box below if you need any assistance.