How to move Azure Resources to another Resource Group

Jorge Bernhardt
Cloud Computing
June 9, 2020

In many cases, it is necessary to reorganize and move Azure resources to other Resource Groups. Today, I want to show you how to move an Azure Resource to another Resource Group using Azure PowerShell.

Important: Not all Azure resources can be moved. for more information, check out this link.

This tutorial assumes that you already have a Microsoft Azure account set up.

Azure PowerShell Workaround

If you want to know how to install the PowerShell Azure module on your machine, check out this link.

The simplest way to get started is to sign in interactively at the command line.

 
  
Connect-AzAccount
 

This cmdlet will bring up a dialog box prompting you for your email address and password associated with your Azure account. If you have more than one subscription associated with your mail account, you can choose the default subscription. To perform this task we will use the following commands:

 
  
Get-AzSubscription 
Select-AzSubscription -Subscription "My Subscription"
 


With the following command in PowerShell, we obtain the list of existing resource groups in your subscription.

 
  
Get-AzResourceGroup | Select-Object ResourceGroupName, Location
 


If you need to create a new resource group, use the New-AzResourceGroup cmdlet with the following syntax.

 
  
New-AzResourceGroup -Name "NEW-RG-DEMO" -Location "North Europe"
 

Set the variables

Here, we define the characteristics of our environment and the resource’s properties.

 
  
#Specifies the names of the resource groups#
$targetRG = "NEW-RG-DEMO"
$sourceRG = "OLD-RG-DEMO"
 


This command obtains the resource in the source resource group.

 
  
$resource = Get-AzResource -Name 'RS-DEMO' -ResourceGroupName $sourceRG
 


Move an Azure Resource

To move an Azure Resource using Azure Powershell, you should use the Move-AzResource cmdlet, with the following syntax:

 
  
Move-AzResource -DestinationResourceGroupName $targetRG `                
								-ResourceId $resource.id `                
								-Force
 


If you want to move the resource to a resource group of another subscription, you must use the

-DestinationSubscriptionId parameter. You must replace “My New Subscription” with the name of the destination subscription.

 
  
$targetSuscription= (Get-AzSubscription -Subscription "My New Suscription")
 

And then use the Move-AzResource cmdlet, with the following syntax:

 
  
Move-AzResource -DestinationResourceGroupName $targetRG `                
								-DestinationSubscriptionId $targetSuscription.id `                
								-ResourceId $resource.id `                 
								-Force
 


If you want to move all existing resources within a resource group, you can use the following commands:

 
  
$resources = Get-AzResource -Name * -ResourceGroupName $sourceRG.ResourceGroupName

foreach ($resource in $resources)
{
 Move-AzResource -DestinationResourceGroupName $targetRG -ResourceId $resource.id                           
                                            -DestinationSubscriptionId $destination.Id                                                                  
                                            -force
}

 

Both the source resource group and the target resource group are locked during the move operation. At the end of the process, the Azure Resource will be in the target resource group.

If you want to know more about this topic, check out this link: https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-move-resources


Jorge Bernhardt

Hi! I am Jorge Bernhardt and I have been an IT professional for almost 20 years. During this time, I have worked as a helpdesk operator, systems administrator, and cloud architect. I am specialised in Microsoft Technologies, particularly Microsoft Cloud & Datacenter solutions, Microsoft Virtualisation, and Microsoft 365. I love learning from other professionals and, when possible, share my knowledge back with the community.

https://www.jorgebernhardt.com

Keep Reading

Newsletter EuropeClouds.com

Thank you! Your submission has been received!

Oops! Something went wrong while submitting the form