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

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...
Azure Site Recovery Azure Site Recovery (ASR) is by far one of the Azure features that I always like to play with. The improvement along the time is above extraordinaire!...
Performing Backup on your DHCP Server In this Tutorial we will be checking the process to protect the DHCP Server database using the automatic configuration, Graphical User Interface and P...
Azure Cloud Shell Everywhere WOW! Microsoft did it again! If you didn’t saw the announcements at the Microsoft Build 2017 Conference, you should! They announce a lot of new thing...