Move Azure virtual Machine to another Vnet

Allan Guerreiro 0 Reputation points
2025-03-11T21:32:09.5433333+00:00

Hello,

I need to change the Vnet from one existing virtual machine, but Azure doesn't have a way to do it.

I built a script to clone the disk of the machine and deploy the machine again on the new network.
Ok, my script works fine.

But the problem is that all information on the Operating system is now missing.
User's image

How can I update that information?

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
8,510 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Sarthak Agarwal 81 Reputation points Microsoft Employee
    2025-03-13T07:44:30.5533333+00:00

    Hi Allan Guerreiro,

    Make sure you are taking a snapshot of the OS disk and then creating VM with set OS disk command:

    Example:
    Taking Snapshot:

    # --- Parameters ---
    $resourceGroupName = "<Your Resource Group Name>"
    $vmName = "<Your VM Name>"
    $snapshotName = "<Desired Snapshot Name>"
    $location = "<Your Azure Region>" # e.g., "eastus"
    # --- Get the VM and OS Disk ID ---
    $vm = Get-AzVM -ResourceGroupName $resourceGroupName -Name $vmName
    $osDiskId = $vm.StorageProfile.OsDisk.ManagedDisk.Id
    # --- Create the Snapshot Configuration ---
    $snapshotConfig = New-AzSnapshotConfig `
        -SourceUri $osDiskId `
        -CreateOption Copy `
        -Location $location
    # --- Create the Snapshot ---
    New-AzSnapshot `
        -Snapshot $snapshotConfig `
        -SnapshotName $snapshotName `
        -ResourceGroupName $resourceGroupName
    

    Create a managed disk from snapshot:
    https://learn.microsoft.com/en-us/azure/virtual-machines/scripts/virtual-machines-powershell-sample-create-managed-disk-from-snapshot#sample-script

    Create a VM from managed disk:
    https://learn.microsoft.com/en-us/previous-versions/azure/virtual-machines/scripts/virtual-machines-powershell-sample-create-vm-from-managed-os-disks?toc=%2Fpowershell%2Fmodule%2Ftoc.json#sample-script

    Regards,
    Sarthak.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.