Skip to main content
Jorge Bernhardt Jorge Bernhardt
  1. Posts/

How to move Azure Resources to another Resource Group

·444 words·3 mins· 100 views · 5 likes ·
Azure PowerShell Connect-AzAccount Get-AzResource Get-AzResourceGroup

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>

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, check out this link.

Set the variables>

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>

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.

Thanks for reading my post. I hope you find it useful.

If you want to know more about this topic, check out this link.