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

MethodNotAllowed when using Backup-AzureKeyVaultSe... I’ve been working with Azure Automation in the last few days, and during the creation of the code I was using PowerShell ISE to backup and restore Azu...
How to move blob data between Storage Accounts usi... In some situations, data from a Storage Account must be moved to a different Storage Account and we can use several options to accomplish this task, s...
How to rename an AWS EC2 instance during provision... In the Cloud era the VM/instance name is not that important, especially when we have a fleet of servers doing the same work, and that is key for scena...
PowerShell Script to fix Exchange Server 2013 Secu... I wrote the following PowerShell script to help fixing the issue reported by Microsoft Exchange Team (http://blogs.technet.com/b/exchange/archive/2013...