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. 

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 moduleYou 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 

Step 2: 

We need to get 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.  

Leaderboard_CDR

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

 

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

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

$connection_string = ‘xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 

$storage_account = New-AzureStorageContext –ConnectionString $connection_string 

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 

$container_name = ‘hubflycontainer1’ 

$blobs = Get-AzureStorageBlob -Container $container_name -Context $storage_account 

Step 4: 

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

-ItemType – Specify the item type as directory 
-Path   Specify the Path where the directory should create 

$destination_path = ‘E:DEMO’ 

 New-Item -ItemType Directory -Force -Path $destination_path 

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 blobs contents should download 
-Context – Specify the context to access the storage BLOBS 

 $blobs = Get-AzureStorageBlob -Container $container_name -Context $storage_account 

    foreach ($blob in $blobs) 

        { 

New-Item -ItemType Directory -Force -Path $destination_path 

            Get-AzureStorageBlobContent  -Container $container_name -Blob $blob.Name -Destination $destination_path -Context $storage_account  

        } 

Now you can check the destination path you will see the blob content will be downloaded. 

Final code: 

$container_name = ‘hubflycontainer 

$destination_path = ‘E:CDN-BACKUPSCDN’ 

$connection_string = ‘xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 

$storage_account = New-AzureStorageContext –ConnectionString $connection_string 

 $blobs = Get-AzureStorageBlob -Container $container_name -Context $storage_account 

foreach ($blob in $blobs) 

{         

New-Item -ItemType Directory -Force -Path $destination_path 

    Get-AzureStorageBlobContent ` 

    -Container $container_name -Blob $blob.Name -Destination $destination_path ` 

-Context $storage_account 

} 

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.