Welcome to Shaun Luttin's public notebook. It contains rough, practical notes. The guiding idea is that, despite what marketing tells us, there are no experts at anything. Sharing our half-baked ideas helps everyone. We're all just muddling thru. Find out more about our work at bigfont.ca.

Managing Azure Resource Groups

Tags: azure, azure-resource-manager, powershell

These commands can be useful to keep related resource organized. For instance, we can delete resource groups we no longer use, or move resources from one group to another.

List the names of Azure resource groups

I like to select just the names for readability.

Get-AzureResourceGroup | select ResourceGroupName

Output

ResourceGroupName            
-----------------            
Default-ClearDB-WestUS       
Default-Media-WestUS         
Default-SQL-WestUS           
Default-Storage-WestUS       
Default-Web-WestUS           
mvp2015                        

Delete a resource group.

This deletes the resource group and its resources.

Remove-AzureResourceGroup -Name $resourceGroupName

# without confirmation
Remove-AzureResourceGroup -Name $resourceGroupName -Force

# bulk operations with a filter
Get-AzureResourceGroup |
  Where-Object { 
    $_.ResourceGroupName -like "Prefix_*" 
  } |
  ForEach-Object { 
    Remove-AzureResourceGroup 
      -Name $_.ResourceGroupName 
      -Force 
  }

Move a resource

First we have to get the details of the resource that we want to move. Resources are organized into types.

List Your resource types

 Get-AzureResource | Group-Object -Property ResourceType | select Name

Output

SuccessBricks.ClearDB/databases
Microsoft.Media/mediaservices
Microsoft.Sql/servers
Microsoft.Sql/servers/databases
Microsoft.ClassicStorage/storageAccounts
Microsoft.AppService/apiapps
Microsoft.AppService/gateways
microsoft.insights/alertrules
microsoft.insights/autoscalesettings
microsoft.insights/components
Microsoft.Web/certificates
Microsoft.Web/serverFarms
Microsoft.Web/sites
Microsoft.Web/sites/slots

Select resources by type

Now that we know the type names, we can select all the Web apps, for instance, like this.

Get-AzureResource -ResourceType Microsoft.Web/sites | `
  select Name, ResourceGroupName | ` 
  Format-Table

Output

Name                                   ResourceGroupName 
----                                   -----------------
Default-Web-WestUS14929cfc35           Default-Web-WestUS
HTTPListenerb35653ca6ebf466b           Default-Web-WestUS
Office365Connector59fdea62cc           Default-Web-WestUS

Move a resource to a different resource group

I like to do this by resource name instead of resource id. This is my approach for now. Note that we need to use -ExpandProperty to make sure we retrieve the string.

Get-AzureResource `
  -ResourceName $resourceName -OutputObjectFormat New | `
    Select -ExpandProperty ResourceId | `
    ForEach-Object { `
        Move-AzureResource -ResourceId $_ `
        -DestinationResourceGroupName $resourceGroupName 
        -Force }

Limitations

We cannot move all Azure resources. For instance, Web Apps have tricky rules. For a full list of solutions, see Move resource to new resource group or subscription.

See Also