How To Move Azure VM To A Different Resource Group PowerShell?

How To Move Azure VM To A Different Resource Group PowerShell

In this Azure tutorial, we will discuss How To Move Azure VM To A Different Resource Group PowerShell?, Along with this, we will also discuss a few other topics like Connect To Azure, Creating Resource Group PowerShell, Creating a virtual network PowerShell, Creating the PublicIP for the VM and we will also discuss Creating the Azure VM PowerShell, Retrieve the list of resource Ids under specific Resource group PowerShell, Use the Azure portal to move a VM to another resource group, etc.

How To Move Azure VM To A Different Resource Group PowerShell? We can move the Azure VM to a different Resource group using the below steps

  1. Connect To Azure using Connect-AzAccount.
  2. Creating Resource Group PowerShell using PowerShell.
  3. Creating a virtual network PowerShell.
  4. Creating the PublicIP for the VM.
  5. Creating the Azure VM PowerShell
  6. Moving the resources to a different resource group

We will discuss the steps in detail moving forward.

How To Move Azure VM To A Different Resource Group PowerShell?

Well, let’s discuss an important topic i.e How To Move Azure VM To A Different Resource Group using PowerShell?. While working with the real-time, there is a chance you will get the requirement where you do have to change the resource group of an Azure Virtual machine to another resource group. Here we will discuss the steps to move Azure VM to a different resource group using PowerShell.

An important thing to note down here is When you are moving one virtual machine from one resource group to another resource group, Behind the scene, the system creates a new resource ID. Make sure to change the resource ID in all the scripts or Code where ever you have used the same resource ID.

What exactly we will do here is, we will create a virtual machine using PowerShell, and then we will move the resource group of the same VM to a different resource group. Before starting the actual functionality, we should know the Prerequisites needed here.

Prerequisites

  • You must have a valid Azure account or Azure Subscription. If you have not created an Azure Account till now. Create a free Azure Account now.
  • Make sure to install the Azure PowerShell module on your machine. If you have not installed the Azure PowerShell module till now, you can install the Azure PowerShell module now.

Assuming, you have all the Prerequisites needed, Let’s start the actual functionality needed here.

Connect To Azure

You can use the below PowerShell cmdlet to connect to Azure.

PS C:\WINDOWS\system32> Connect-AzAccount

Once, you will execute the above PowerShell cmdlet, it will prompt you to provide your Azure Account credentials. Enter your credentials and you should get the Output like below

Account                   SubscriptionName         TenantId                           
-------                   ----------------         --------                           
[email protected] Visual Studio Enterprise 5d9d690a-xxxx-47xx-ae8b-42df2d54...

Creating Resource Group PowerShell

Let’s create two resource groups here, as we need two resource groups. You can use the below PowerShell cmdlet to create the resource group.

New-AzResourceGroup -Name DemoVM1 -Location "East US"

Here, DemoVM1 is the resource group name and East US is the location of the resource group. Once we have executed the above PowerShell script, you can able to see, the resource group DemoVM1 is successfully created.

powershell move vm to resource group

The Output is as below

ResourceGroupName : DemoVM1
Location          : eastus
ProvisioningState : Succeeded
Tags              : 
ResourceId        : /subscriptions/1cdf4300-dee5-4518-9c9c-feaa72a5cbd1/resourceGroups
                    /DemoVM1

Now, we need to create the second resource group using the below PowerShell cmdlet.

New-AzResourceGroup -Name DemoVM2 -Location "East US"

Here, DemoVM2 is the resource group name and East US is the location of the resource group. Once we have executed the above PowerShell script, you can able to see, the resource group DemoVM2 is successfully created.

Move a Windows VM to another Azure subscription or resource group

The Output is as below

ResourceGroupName : DemoVM2
Location          : eastus
ProvisioningState : Succeeded
Tags              : 
ResourceId        : /subscriptions/1cdf4300-dee5-4518-9c9c-feaa72a5cbd1/resourceGroups
                    /DemoVM2

Creating a virtual network PowerShell

As, we are going to create a virtual machine here, so we need t to create a virtual network. As the next step, we are going to create a Virtual Network using PowerShell under the first resource group. You can use the below PowerShell script to create the Virtual network.

$mysubnet  = New-AzVirtualNetworkSubnetConfig -Name myvmsubnet -AddressPrefix "10.0.2.0/24"
New-AzVirtualNetwork -Name TsinfoNewVN -ResourceGroupName DemoVM1 -Location "East US" -AddressPrefix "10.0.0.0/16" -Subnet $mysubnet

Once, you will run the above script, you can able to see we got the expected output and the Virtual network is created successfully

Move Azure VMs to new subscription or resource group

The complete Output is as below

Name                   : TsinfoNewVN
ResourceGroupName      : DemoVM1
Location               : eastus
Id                     : /subscriptions/1cdf4300-dee5-4518-9c9c-feaa72a5cbd1/resourceG
                         roups/DemoVM1/providers/Microsoft.Network/virtualNetworks/Tsi
                         nfoNewVN
Etag                   : W/"e1e2d01c-4ff7-4d96-b270-60183bb26864"
ResourceGuid           : 7f060238-9ee7-4c89-923a-b8ba866972f4
ProvisioningState      : Succeeded
Tags                   : 
AddressSpace           : {
                           "AddressPrefixes": [
                             "10.0.0.0/16"
                           ]
                         }
DhcpOptions            : {}
Subnets                : [
                           {
                             "Delegations": [],
                             "Name": "myvmsubnet",
                             "Etag": "W/\"e1e2d01c-4ff7-4d96-b270-60183bb26864\"",
                             "Id": "/subscriptions/1cdf4300-dee5-4518-9c9c-feaa72a5cbd
                         1/resourceGroups/DemoVM1/providers/Microsoft.Network/virtualN
                         etworks/TsinfoNewVN/subnets/myvmsubnet",
                             "AddressPrefix": [
                               "10.0.2.0/24"
                             ],
                             "IpConfigurations": [],
                             "ServiceAssociationLinks": [],
                             "ResourceNavigationLinks": [],
                             "ServiceEndpoints": [],
                             "ServiceEndpointPolicies": [],
                             "PrivateEndpoints": [],
                             "ProvisioningState": "Succeeded",
                             "PrivateEndpointNetworkPolicies": "Enabled",
                             "PrivateLinkServiceNetworkPolicies": "Enabled",
                             "IpAllocations": []
                           }
                         ]
VirtualNetworkPeerings : []
EnableDdosProtection   : false
DdosProtectionPlan     : null

Creating the PublicIP for the VM

You can use the below PowerShell script to create the PublicIP for the virtual machine that we can use to connect the Virtual machine externally.

New-AzPublicIpAddress -Name TSINFOVMIP -ResourceGroupName DemoVM -Location “East US” -AllocationMethod Dynamic

You can able to see, the script got executed successfully without any issue

Move a virtual machine to a resource group

The complete output is as below

Name                     : TSINFOVMIP
ResourceGroupName        : DemoVM
Location                 : eastus
Id                       : /subscriptions/1cdf4300-dee5-4518-9c9c-feaa72a5cbd1/resourc
                           eGroups/DemoVM/providers/Microsoft.Network/publicIPAddresse
                           s/TSINFOVMIP
Etag                     : W/"46211576-41a8-4362-8d3a-51d7c4eb2855"
ResourceGuid             : e8294171-cbca-441c-8197-a012fea550b3
ProvisioningState        : Succeeded
Tags                     : 
PublicIpAllocationMethod : Dynamic
IpAddress                : Not Assigned
PublicIpAddressVersion   : IPv4
IdleTimeoutInMinutes     : 4
IpConfiguration          : null
DnsSettings              : null
Zones                    : {}
Sku                      : {
                             "Name": "Basic",
                             "Tier": "Regional"
                           }
IpTags                   : []

Now, our Resource group and Virtual Network, and PublicIP for the VM are ready, the time to create the Virtual machine.

Creating the Azure VM PowerShell

You can use the below PowerShell script to create the Azure Virtual machine. We will create the Virtual machine under the DemoVM1 resource group. Later on, we will move the resource group of the Virtual machine from DemoVM1 to DemoVM2.

$loginDetails = Get-Credential
New-AzVm -ResourceGroupName DemoVM1 -Name "TSINFOVM7" -Location "East US" -VirtualNetworkName "TsinfoNewVN" -SubnetName "myvmsubnet" -addressprefix 10.0.2.0/24 -PublicIpAddressName "TSINFOVMIP" -OpenPorts 3389 -Image win2019datacenter -Size Standard_D2s_v3 -Credential $loginDetails

You can able to see below, the script executed successfully without any issue, and the Virtual machine created successfully and ready to use now.

azure move vm to different resource group

The complete Output is as below

ResourceGroupName        : DemoVM1
Id                       : /subscriptions/1cdf4300-dee5-4518-9c9c-feaa72a5cbd1/resourc
eGroups/DemoVM1/providers/Microsoft.Compute/virtualMachines/TSINFOVM7
VmId                     : 26df0870-dc93-44eb-b8f0-b5aea598baf4
Name                     : TSINFOVM7
Type                     : Microsoft.Compute/virtualMachines
Location                 : eastus
Tags                     : {}
HardwareProfile          : {VmSize}
NetworkProfile           : {NetworkInterfaces}
OSProfile                : {ComputerName, AdminUsername, WindowsConfiguration, 
Secrets, AllowExtensionOperations, RequireGuestProvisionSignal}
ProvisioningState        : Succeeded
StorageProfile           : {ImageReference, OsDisk, DataDisks}
FullyQualifiedDomainName : tsinfovm7-528f2c.East US.cloudapp.azure.com

So, Our virtual machine is ready now and we have created under the DemoVM1 resource group.

Check Out the Resources Under the Resource Group

Now, let’s see the list of resources under the DemoVM1 resource group. To check that, you can use the below PowerShell script.

PS C:\WINDOWS\system32> Get-AzResource -ResourceGroupName DemoVM1

You can see below, the query executed successfully and we got the expected Output

How to move resources between Resource groups in Azure using PowerShell

The complete Output is as below

Name              : TSINFOVM7_disk1_13a756a895f54bf1962557c36a4212a7
ResourceGroupName : DEMOVM1
ResourceType      : Microsoft.Compute/disks
Location          : eastus
ResourceId        : /subscriptions/1cdf4300-dee5-4518-9c9c-feaa72a5cbd1/resourceGroups
                    /DEMOVM1/providers/Microsoft.Compute/disks/TSINFOVM7_disk1_13a756a
                    895f54bf1962557c36a4212a7
Tags              : 

Name              : TSINFOVM7
ResourceGroupName : DemoVM1
ResourceType      : Microsoft.Compute/virtualMachines
Location          : eastus
ResourceId        : /subscriptions/1cdf4300-dee5-4518-9c9c-feaa72a5cbd1/resourceGroups
                    /DemoVM1/providers/Microsoft.Compute/virtualMachines/TSINFOVM7
Tags              : 

Name              : TSINFOVM7
ResourceGroupName : DemoVM1
ResourceType      : Microsoft.Network/networkInterfaces
Location          : eastus
ResourceId        : /subscriptions/1cdf4300-dee5-4518-9c9c-feaa72a5cbd1/resourceGroups
                    /DemoVM1/providers/Microsoft.Network/networkInterfaces/TSINFOVM7
Tags              : 

Name              : TSINFOVM7
ResourceGroupName : DemoVM1
ResourceType      : Microsoft.Network/networkSecurityGroups
Location          : eastus
ResourceId        : /subscriptions/1cdf4300-dee5-4518-9c9c-feaa72a5cbd1/resourceGroups
                    /DemoVM1/providers/Microsoft.Network/networkSecurityGroups/TSINFOV
                    M7
Tags              : 

Name              : TSINFOVMIP
ResourceGroupName : DemoVM1
ResourceType      : Microsoft.Network/publicIPAddresses
Location          : eastus
ResourceId        : /subscriptions/1cdf4300-dee5-4518-9c9c-feaa72a5cbd1/resourceGroups
                    /DemoVM1/providers/Microsoft.Network/publicIPAddresses/TSINFOVMIP
Tags              : 

Name              : TsinfoNewVN
ResourceGroupName : DemoVM1
ResourceType      : Microsoft.Network/virtualNetworks
Location          : eastus
ResourceId        : /subscriptions/1cdf4300-dee5-4518-9c9c-feaa72a5cbd1/resourceGroups
                    /DemoVM1/providers/Microsoft.Network/virtualNetworks/TsinfoNewVN
Tags              : 

Our requirement here is to move the Azure VM from one resource group to another so in order to do that we will have to move all the resources under the resource group and To move the resources we need to get all the resources Ids. You can use the below PowerShell script to get all the resource IDs.

Retrieve the list of resource Ids under specific Resource group PowerShell

PS C:\WINDOWS\system32> Get-AzResource -ResourceGroupName DEMOVM1 | Format-list -Property ResourceId

Once, we have executed the above query, you can able to see, we got the list of resource Ids under the DEMOVM1 resource group.

azure change vm to different resource group

The complete Output is as below

ResourceId : /subscriptions/1cdf4300-dee5-4518-9c9c-feaa72a5cbd1/resourceGroups/DEMOVM
             1/providers/Microsoft.Compute/disks/TSINFOVM7_disk1_13a756a895f54bf196255
             7c36a4212a7

ResourceId : /subscriptions/1cdf4300-dee5-4518-9c9c-feaa72a5cbd1/resourceGroups/DemoVM
             1/providers/Microsoft.Compute/virtualMachines/TSINFOVM7

ResourceId : /subscriptions/1cdf4300-dee5-4518-9c9c-feaa72a5cbd1/resourceGroups/DemoVM
             1/providers/Microsoft.Network/networkInterfaces/TSINFOVM7

ResourceId : /subscriptions/1cdf4300-dee5-4518-9c9c-feaa72a5cbd1/resourceGroups/DemoVM
             1/providers/Microsoft.Network/networkSecurityGroups/TSINFOVM7

ResourceId : /subscriptions/1cdf4300-dee5-4518-9c9c-feaa72a5cbd1/resourceGroups/DemoVM
             1/providers/Microsoft.Network/publicIPAddresses/TSINFOVMIP

ResourceId : /subscriptions/1cdf4300-dee5-4518-9c9c-feaa72a5cbd1/resourceGroups/DemoVM
             1/providers/Microsoft.Network/virtualNetworks/TsinfoNewVN

Moving the resources to a different resource group

Now, we can move all our resources to the 2nd resource group using the below PowerShell cmdlet.

Move-AzResource -DestinationResourceGroupName DEMOVM2 -ResourceId id1,id2,id3

In my case, the PowerShell script will be like below

Move-AzResource -DestinationResourceGroupName DEMOVM2 -ResourceId /subscriptions/1cdf4300-dee5-4518-9c9c-feaa72a5cbd1/resourceGroups/DEMOVM1/providers/Microsoft.Compute/disks/TSINFOVM7_disk1_13a756a895f54bf1962557c36a4212a7, /subscriptions/1cdf4300-dee5-4518-9c9c-feaa72a5cbd1/resourceGroups/DemoVM1/providers/Microsoft.Compute/virtualMachines/TSINFOVM7, /subscriptions/1cdf4300-dee5-4518-9c9c-feaa72a5cbd1/resourceGroups/DemoVM1/providers/Microsoft.Network/networkInterfaces/TSINFOVM7, /subscriptions/1cdf4300-dee5-4518-9c9c-feaa72a5cbd1/resourceGroups/DemoVM1/providers/Microsoft.Network/networkSecurityGroups/TSINFOVM7, /subscriptions/1cdf4300-dee5-4518-9c9c-feaa72a5cbd1/resourceGroups/DemoVM1/providers/Microsoft.Network/publicIPAddresses/TSINFOVMIP, /subscriptions/1cdf4300-dee5-4518-9c9c-feaa72a5cbd1/resourceGroups/DemoVM1/providers/Microsoft.Network/virtualNetworks/TsinfoNewVN

Once you will execute the above commands you will get the below pop up, click on the Yes button to confirm

How do I transfer resources from one resource group to another in Azure?

In case, you have more resources to migrate, you might get a timeout error. In that case, you will have to run the above PowerShell script again and again

For me, the query has been executed successfully and all the Azure resources are moved to the other resource group without any issue.

Now, to cross-check that the resources have been moved successfully, we need to check the second resource group i.e DemoVM2 in my case. You can use the below PowerShell script to check that

PS C:\WINDOWS\system32> Get-AzResource -ResourceGroupName DEMOVM2

Once we have executed the above script, you can able to see all the Azure resources are now present under the second resource group i.e DEMOVM2

azure move resource to another resource group

The complete output is like below

Name              : TSINFOVM7_disk1_13a756a895f54bf1962557c36a4212a7
ResourceGroupName : DemoVM2
ResourceType      : Microsoft.Compute/disks
Location          : eastus
ResourceId        : /subscriptions/1cdf4300-dee5-4518-9c9c-feaa72a5cbd1/resourceGroups
                    /DemoVM2/providers/Microsoft.Compute/disks/TSINFOVM7_disk1_13a756a
                    895f54bf1962557c36a4212a7
Tags              : 

Name              : TSINFOVM7
ResourceGroupName : DemoVM2
ResourceType      : Microsoft.Compute/virtualMachines
Location          : eastus
ResourceId        : /subscriptions/1cdf4300-dee5-4518-9c9c-feaa72a5cbd1/resourceGroups
                    /DemoVM2/providers/Microsoft.Compute/virtualMachines/TSINFOVM7
Tags              : 

Name              : TSINFOVM7
ResourceGroupName : DemoVM2
ResourceType      : Microsoft.Network/networkInterfaces
Location          : eastus
ResourceId        : /subscriptions/1cdf4300-dee5-4518-9c9c-feaa72a5cbd1/resourceGroups
                    /DemoVM2/providers/Microsoft.Network/networkInterfaces/TSINFOVM7
Tags              : 

Name              : TSINFOVM7
ResourceGroupName : DemoVM2
ResourceType      : Microsoft.Network/networkSecurityGroups
Location          : eastus
ResourceId        : /subscriptions/1cdf4300-dee5-4518-9c9c-feaa72a5cbd1/resourceGroups
                    /DemoVM2/providers/Microsoft.Network/networkSecurityGroups/TSINFOV
                    M7
Tags              : 

Name              : TSINFOVMIP
ResourceGroupName : DemoVM2
ResourceType      : Microsoft.Network/publicIPAddresses
Location          : eastus
ResourceId        : /subscriptions/1cdf4300-dee5-4518-9c9c-feaa72a5cbd1/resourceGroups
                    /DemoVM2/providers/Microsoft.Network/publicIPAddresses/TSINFOVMIP
Tags              : 

Name              : TsinfoNewVN
ResourceGroupName : DemoVM2
ResourceType      : Microsoft.Network/virtualNetworks
Location          : eastus
ResourceId        : /subscriptions/1cdf4300-dee5-4518-9c9c-feaa72a5cbd1/resourceGroups
                    /DemoVM2/providers/Microsoft.Network/virtualNetworks/TsinfoNewVN
Tags              : 

You can able to see, we have moved all the Azure resources successfully with out any issue.

This is How To Move Azure VM To A Different Resource Group PowerShell? by following the above instruction.

Use the Azure portal to move a VM to another resource group

We have discussed above, How to use Powershell to move a VM to another resource group. Now, let’s see How to use the Azure portal to move a VM to another resource group. Follow the below steps to move the Azure VM.

Step-1: Login to the Azure Portal (https://portal.azure.com/).

Step-2: Search for the “Virtual machines”, you can able to see the list of Azure Virtual machines you have. Click on the Resource group link of the respective virtual machine that you want to move to another resource group.

DEMOVM is the name of my Resource group and I want to move the TSINFOVM to another resource group named DEMOVM2.

Use the Azure portal to move a VM to another resource group

Step-3: On the Resource Group page, click on the Move option and then select Move to another resource group which is present at the top of the page for the resource group as shown below.

How to Use the Azure portal to move a VM to another resource group

Step-3: Select all the resources that are related to your VM. In many cases, you need to select all the resources. Then select the new resource group where you want to move the Virtual machine or you can also click on the Create a new group link to create a new resource group, Accept the terms, and then click on the Ok button to confirm.

how to move azure vm to another resource group

This is how You can move a VM and its associated resources to another resource group by using the Azure portal.

You may also like following the below article

Wrapping Up

Well, in this article, we have discussed, How To Move Azure VM To A Different Resource Group PowerShell?, Connect To Azure, Creating Resource Group PowerShell, Creating a virtual network PowerShell, Creating the PublicIP for the VM, and we have also discussed Creating the Azure VM PowerShell, Retrieve the list of resource Ids under specific Resource group PowerShell, Use the Azure portal to move a VM to another resource group, etc. Hope you have enjoyed this article !!!.