Looping through your Azure subscriptions using PowerShell

Share this:

When working with multiple subscriptions in Azure, more than often we will have to loop through the available subscriptions to list or change resources.

Solution


The solution is simple, basically we use a variable to receive all existent subscriptions (make sure that you are connected into your Azure), and then we loop every subscription and inside of that ForEach we can run our cmdlets.

Here is a piece of code that does exactly that. If you want help to connect in your azure, you can use Connect-AzureRMAccount.

$subscriptions=Get-AzureRMSubscription

ForEach ($vsub in $subscriptions){
Select-AzureRmSubscription $vsub.SubscriptionID

Write-Host

Write-Host “Working on “ $vsub

Write-Host

Get-AzureRmVM | Select Name,REsourceGroupName,Location

}

Written by Anderson Patricio

Anderson Patricio

Anderson Patricio is a Canadian MVP in Cloud and Datacenter Management, and Office Server and Services, besides the Microsoft Award he also holds a Solutions Master (MCSM) in Exchange and several other certifications. Anderson has been contributing to the Microsoft Community with articles, tutorials, blog posts, twitter, forums and book reviews. He is a regular contributor here at ITPROCentral.com, MSExchange.org, Techgenix.com and Anderson Patricio.org (Portuguese).

Related Post

AAD Sync – Modifying a filter rule How to modify an existent filter rule and validating what changes in the Azure Active Directory using AAD Sync. Solution To demonstrate how a ...
Azure Backup – Part 4 – System Center and Azure Ba... On the first post (see here), I explained how the Azure backup works. On this post, I’m explaining how to integrate Azure Backup with System Center Da...
How to remove the several Organization Units that ... In some situation an entire structure of Organization Units must be deleted, and by default new OUs are automatic defined to protect them against dele...
Removing Azure Active Directory users using Azure ... Cloud Shell is one of the new features in Azure Portal, and it is accessible from virtually any browser and even from Azure App. In this Tutorial, we ...