The concept of resource groups has been around for a little while, and is adequately supported in the Azure preview portal. Resource groups are logical containers that allow you to group individual resources such as virtual machines, storage accounts, websites and databases so they can be managed together. They give a much clearer picture to what resources belong together, and can also give visibility into consumption/spending in a grouped matter.

However, when resources are created in the classic Azure portal (e.g. virtual machines, storage accounts, etc.) there is no support for resource group management, which results in a new resource group being created for each resource that you create. This can lead to a large number of resource groups that are unclear and tedious to manage. Also, if you do tend to use resource groups in the Azure preview portal there is no way to perform housekeeping or management of these resource groups.

With the latest Azure PowerShell cmdlets (v0.8.15.1) we now have the ability to move resources between resource groups. You can install the latest version of the PowerShell tools via the Web Platform Installer:

wpi azure powershell

After installation of this particular version we now have the following PowerShell commands available that will assist us in moving resources:

  • New-AzureResourceGroup
  • Move-AzureResource
  • Remove-AzureResourceGroup
  • Get-AzureResource
  • Get-AzureResourceGroup
  • Get-AzureResourceLog
  • Get-AzureResourceGroupLog

Switch-AzureMode AzureResourceManager

After launching a Microsoft Azure Powershell console we need to switch to Azure Resource Manager mode in order to manage our resource groups:

Switch-AzureMode AzureResourceManager

Get-AzureResourceGroup

Without any parameters this cmdlet gives a complete list of all resource groups that are deployed in your current subscription:

When resources are created in the classic Azure portal they will appear with a new resource group name that corresponds to the name of the object that was created (e.g. virtual machine name, storage account name, website name, etc.).

Note that we have a few default resource groups for storage, SQL and some specific resource groups corresponding to virtual machines. These were automatically created when I built some virtual machines and a Azure SQL server database in the classic Azure portal.

New-AzureResourceGroup

In order to group our existing resources we’re going to create a new resource group. It’s important to note that resource groups reside in a particular region which needs to be specified upon creation:

You’d think that resources can only be moved across resource groups that reside in the same region. However, I’ve successfully moved resources between resource groups that reside in different regions. This doesn’t affect the actual location of the resource so I’m not sure what the exact purpose of specifying a location for a resource group is.

Get-AzureResourceGroup

The Get-AzureResourceGroup cmdlet allows you to view all resources within a group, including their respective types and IDs:

Move-AzureResourceGroup

To move resources from the existing resource groups we need to provide the Move-ResourceGroup cmdlet a list of resource IDs. The cmdlet accepts the resource ID(s) as pipeline input parameters, so we can use the Get-AzureResource cmdlet to feed the list of resource IDs. The following script moves a cloud service, virtual machine and storage account (all residing in the same region) to the newly created resource group:

The Get-AzureResource cmdlet allows you to further filter based on resource type, or individual resource name. The Move-ResourceGroup cmdlet automatically removed the original resource group in case there are no resources associated after moving them.

Unfortunately at the time of writing there was an issue with moving SQL database servers and databases to other resource groups:

Trying to move the SQL server only does not raise any errors, but doesn’t result in the desired target state and leaves the SQL server and database in the original resource group:

The cmetlets Get-AzureResourceLog and Get-AzureResourceGroupLog provide a log of all the performed operations on resources and resource groups, but couldn’t provide any further information regarding the failure to move resources to the new group.

Now we have successfully moved our virtual machine and storage account to the new resource group we can get insight into these resources through the resource group:

Resource Group

Category:
Azure Platform
Tags:
,

Join the conversation! 16 Comments

  1. These Cmdlets don’t seem to be available in the latest Azure powershell released 4/5/2015 (4th May)

  2. I have Azure PowerShell v0.9.1 installed.
    I run
    Get-AzureResource -ResourceGroupName SrcRG | Move-AzureResource -DestinationResourceGroupName DstRG
    but it does and says nothing.

    • What do you get if you invoke Get-AzureResource -ResourceGroupName SrcRG on it’s own? Don’t forget you can also use Get-AzureResourceLog to view logs from the requested operation. You should see an operation timeout if the command fails – do you see a timeout (it will show as an Exception)?

  3. Get-AzureResource -ResourceGroupName ResourceGroup | Move-AzureResource -DestinationResourceGroupName MyResourceGroup
    I also get nothing. The storage I have in my default ResourceGroup still there, not get moved to MyResourceGroup, why?

    • Hi Frank, does the Get-AzureResource return anything at all? What exactly do you mean by ‘I also get nothing’?

  4. I have the same issue as Vadimmitin.
    “Get-AzureResource -ResourceGroupName SrcRG” gives me the list of SubscriptionId’s.
    Vadimmitin did you find a solution for it?

    • Cammeraat – if you look closely you will see that it returns a list of ResourceIds (not SubscriptionIds). This output can be piped directly into the Move-AzureResource Cmdlet as per Olaf’s blog above.

  5. same here, it doesn’t move the resource

  6. Worked for me:
    “`
    Get-AzureResource -ResourceGroupName $originalResourceGroup | where { $_.ResourceType -match ‘Microsoft.ClassicCompute’ } | Move-AzureResource -DestinationResourceGroupName $newResourceGroup
    “`

  7. Great Article. thank you!

    Just my option but until data resources can be moved, this features is at best misleading, and at worst a waste of half a day’s research. Unfortunately, I didn’t discover your section on moving data resources until after banging my head off a wall for an hour.

    Also the names seem to have changed again. Moving simple resources worked this way:

    Get-AzureRmResource -ResourceName myResource -ResourceGroupName SrcResourceGroup | Move-AzureRmResource -DestinationResourceGroupName myDestResourceGroup

    • This!

    • Thanks Michael. this worked for me.

      ModuleType Version Name ExportedCommands
      ———- ——- —- —————-
      Manifest 1.0.3 Azure {Disable-AzureServiceProjectRemoteDesktop, Enable-AzureSer…

      OS Name: Microsoft Windows 10 Enterprise
      OS Version: 10.0.14393 N/A Build 14393

      PSVersion 5.1.14393.187
      PSEdition Desktop
      PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
      BuildVersion 10.0.14393.187
      CLRVersion 4.0.30319.42000
      WSManStackVersion 3.0
      PSRemotingProtocolVersion 2.3
      SerializationVersion 1.1.0.1

  8. I am getting this error
    The term ‘Get-AzureResourceGroup’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At line:1 char:1

    Do I have to install azure SDK? I can not install SDK on client machine

Comments are closed.