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

Windows Azure e Office365 – Validating the synchro... In this final article of this Tutorial Series we will go over the process to create an user on-premises and what we have to do on our side to enable s...
How to move your workloads to Azure When integrating your on-premises environment with Azure, you might want to use the lift-and-shift approach to migrate some of your existing workloads...
Updates on Hyper-V Server? How to see what had bee... When do you need to see what updates had been installed on your Hyper-V server, you can use this PowerShell cmdlet to list all the updates. Get-WmiOb...
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 ...