Skip to content

Instantly share code, notes, and snippets.

@nexocentric
Created February 12, 2026 16:35
Show Gist options
  • Select an option

  • Save nexocentric/8c9e4346587f4148273bf9c89cef0b1e to your computer and use it in GitHub Desktop.

Select an option

Save nexocentric/8c9e4346587f4148273bf9c89cef0b1e to your computer and use it in GitHub Desktop.
# Requires: Az.Accounts, Az.Storage
# --- Inputs ---
$SubscriptionId = "<sub-id>" # optional (if you want to force a sub)
$ResourceGroupName = "<rg-name>"
$StorageAccountName = "<storage-account-name>" # single SA target
# Container filters (choose one style)
$ContainerNames = @("container-a","container-b") # exact list (optional)
$ContainerPatterns = @("logs-*","exports-*") # wildcard list (optional)
$DestinationRoot = "C:\downloads\storage" # local root folder
$Overwrite = $true # overwrite local files if exists
# --- Login / context ---
Connect-AzAccount | Out-Null
if ($SubscriptionId -and $SubscriptionId -ne "<sub-id>") {
Set-AzContext -SubscriptionId $SubscriptionId | Out-Null
}
# --- Filter to ONE storage account ---
$sa = Get-AzStorageAccount -ResourceGroupName $ResourceGroupName |
Where-Object StorageAccountName -EQ $StorageAccountName |
Select-Object -First 1
if (-not $sa) { throw "Storage account '$StorageAccountName' not found in RG '$ResourceGroupName'." }
$ctx = $sa.Context
# --- List containers and filter to the set you want ---
$containers = Get-AzStorageContainer -Context $ctx
if ($ContainerNames -and $ContainerNames.Count -gt 0) {
$containers = $containers | Where-Object { $ContainerNames -contains $_.Name }
}
if ($ContainerPatterns -and $ContainerPatterns.Count -gt 0) {
$containers = $containers | Where-Object {
$name = $_.Name
$ContainerPatterns | Where-Object { $name -like $_ } | Select-Object -First 1
}
}
if (-not $containers) { throw "No containers matched the provided filters." }
# --- Download all blobs from each container ---
foreach ($c in $containers) {
$containerName = $c.Name
Write-Host "==> Container: $containerName"
$containerDest = Join-Path $DestinationRoot $containerName
New-Item -ItemType Directory -Path $containerDest -Force | Out-Null
# Enumerate blobs (handles paging under the hood)
$blobs = Get-AzStorageBlob -Container $containerName -Context $ctx
foreach ($b in $blobs) {
# Preserve "virtual folders" in blob names
$relativePath = $b.Name -replace '/', '\'
$localPath = Join-Path $containerDest $relativePath
$localDir = Split-Path -Parent $localPath
if ($localDir) { New-Item -ItemType Directory -Path $localDir -Force | Out-Null }
if (-not $Overwrite -and (Test-Path $localPath)) {
Write-Host " Skipping existing: $($b.Name)"
continue
}
Write-Host " Downloading: $($b.Name)"
Get-AzStorageBlobContent `
-Container $containerName `
-Blob $b.Name `
-Destination $localPath `
-Context $ctx `
-Force | Out-Null
}
}
Write-Host "Done. Downloaded to: $DestinationRoot"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment