1 min read

Run Azure CLI commands across multiple subscriptions


When you manage more than one subscription

If you manage multiple Azure subscriptions you’ve probably found yourself running az account set over and over. Here’s a better way.

Loop through all subscriptions and list resource groups in each:

loop_subscriptions.sh
for sub in $(az account list --query "[].id" -o tsv); do
  echo "=== Subscription: $sub ==="
  az account set --subscription "$sub"
  az group list --query "[].{Name:name, Location:location}" -o table
done

Want to find a specific resource across all subscriptions? Swap the inner command:

find_resource_across_subs.sh
for sub in $(az account list --query "[].id" -o tsv); do
  az account set --subscription "$sub"
  az resource list --query "[?contains(name, 'myapp')].{Name:name, Sub:'$sub'}" -o table
done

Pro tip: For read-only queries across subscriptions, Azure Resource Graph is even faster:

resource_graph.sh
az graph query -q "Resources | where name contains 'myapp' | project name, subscriptionId, resourceGroup" -o table