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.
- ################# Azure Blob Storage - PowerShell ####################
-
- ## Input Parameters
- $resourceGroupName="azpractice"
- $storageAccName="azstorageacc1122020"
- $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 content
- 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 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.