How to download blob contents from Azure Storage Account using PowerShell

In this article, you will see how to download blob contents from Azure Storage Account using PowerShell.


Install the Azure PowerShell Module:

Open Windows PowerShell window and run the below command.


Install-Module -Name Az –AllowClobber


Download Blob Contents:

Open a text file. Copy and paste the below script. Save the file as script.ps1.


################# Azure Blob Storage - PowerShell ####################

## Input Parameters

$resourceGroupName="psspschennairg"

$storageAccName="psspschennaistorageacc"

$downloadPath=".\Download"

$downloadLocation="Download"


## Connect to Azure Account

Connect-AzAccount


## Function to dlownload all blob contents

Function DownloadBlobContents

{

Write-Host -ForegroundColor Green "Download blob contents from storage container.."

## Get the storage account

$storageAcc=Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccName

## Get the storage account context

$ctx=$storageAcc.Context

## Get all the containers

$containers=Get-AzStorageContainer -Context $ctx

## Loop through the containers

foreach($container in $containers)

{

## check if folder exists

$folderPath=$downloadPath+"\"+$container.Name

$destination=$downloadLocation+"\"+$container.Name

$folderExists=Test-Path -Path $folderPath

if($folderExists)

{

Write-Host -ForegroundColor Magenta $container.Name "- folder exists"

## Get the blob contents from the container

$blobContents=Get-AzStorageBlob -Container $container.Name -Context $ctx

foreach($blobContent in $blobContents)

{

## Download the blob contentFor

Get-AzStorageBlobContent -Container $container.Name -Context $ctx -Blob $blobContent.Name -Destination $destination -Force

}

}

else

{

Write-Host -ForegroundColor Magenta $container.Name "- folder does not exist"

## Create the new folder

New-Item -ItemType Directory -Path $folderPath

## Get the blob contents from the container

$blobContents=Get-AzStorageBlob -Container $container.Name -Context $ctx

foreach($blobContent in $blobContents)

{

## Download the blob content

Get-AzStorageBlobContent -Container $container.Name -Context $ctx -Blob $blobContent.Name -Destination $destination -Force

}

}

}

}


DownloadBlobContents


## Disconnect from Azure Account

Disconnect-AzAccount

######################################################################


Open 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 Azure Storage account downloaded successfully as shown below. Note: Folders will be created with the same name as container name and if folder exists documents gets downloaded in the respective folder.




Summary:

Thus in this article you saw how to download blob contents from Azure Storage Account using PowerShell.