Create a VM from a snapshot in Azure using PowerShell

Recently, I was working on a requirement that I was supposed to create an Azure VM from a snapshot using PowerShell. This Azure tutorial will discuss the step-by-step process of creating an Azure Virtual Machine from a snapshot using PowerShell script.

Create an Azure VM from a snapshot using PowerShell

Create a Virtual Machine in Azure using PowerShell, If you have not done so yet.

Now, let’s log in to the Azure virtual machine we created. We will create two text files and keep them on the Desktop.

create snapshot azure vm powershell

So now, Our Azure Virtual machine is ready with the test files.

The next step is to create a snapshot of Azure VM using PowerShell. If you have not yet created the snapshot of your Azure VM.

How to Create Azure Virtual Machine From Snapshot

Well, we have created the Azure Windows Virtual Machine Snapshot above. Now, We will be Creating an Azure Windows Virtual Machine From Snapshot.

Getting the snapshot data

Let’s create a variable that will hold the snapshot data. Use the below PowerShell cmdlet to create the variable that will hold the snapshot data.

$snapshot = Get-AzSnapshot -ResourceGroupName DemoVM -SnapshotName tsinfosnap

Creating the managed disk using PowerShell

We must create the managed disk using the above snapshot for the new VM.

$mydskconfig = New-AzDiskConfig -Location “East US” -SourceResourceId $snapshot.Id -CreateOption Copy

$newdisk = New-AzDisk -Disk $mydskconfig -ResourceGroupName DemoVM -DiskName tsinfoDisk

Initializing virtual machine configuration

As the next step, initialize the virtual machine configuration using the below PowerShell cmdlet.

$tsinfovmconfig = New-AzVMConfig -VMName TSINFOVMNEW -VMSize Standard_D2s_v3
$tsinfovmconfig = Set-AzVMOSDisk -VM $tsinfovmconfig -ManagedDiskId $newdisk.Id -CreateOption Attach -Windows

Creating the PublicIP for the VM using PowerShell

You can use the below PowerShell cmdlet for the PublicIP configuration.

$publicip = New-AzPublicIpAddress -Name TSINFONEWVMIP -ResourceGroupName DemoVM -Location “East US” -AllocationMethod Dynamic

Creating NIC for the new VM using PowerShell

Use the below PowerShell cmdlet to create the NIC for the virtual machine.

$myvnet = Get-AzVirtualNetwork -Name TSINFOVN1 -ResourceGroupName DemoVM

$mysubnet = Get-AzVirtualNetworkSubnetConfig -Name vmsubnet -VirtualNetwork $myvnet
$vmnic = New-AzNetworkInterface -Name “TSINFONEWVM_nic1” -ResourceGroupName DemoVM -Location “East US” -SubnetId $mysubnet.Id -PublicIpAddressId $publicip.Id

Creating the Azure Virtual Machine PowerShell

As the next step, let’s add the newly created NIC to the VM configuration and create the Virtual machine using the below PowerShell cmdlet.

$tsinfovmconfig = Add-AzVMNetworkInterface -VM $tsinfovmconfig -Id $vmnic.Id
New-AzVM -VM $tsinfovmconfig -ResourceGroupName DemoVM -Location “East US”

Below is the complete PowerShell script that you can execute

$snapshot = Get-AzSnapshot -ResourceGroupName DemoVM -SnapshotName tsinfosnap
$mydskconfig = New-AzDiskConfig -Location “East US” -SourceResourceId $snapshot.Id -CreateOption Copy

$newdisk = New-AzDisk -Disk $mydskconfig -ResourceGroupName DemoVM -DiskName tsinfoDisk

$tsinfovmconfig = New-AzVMConfig -VMName TSINFOVMNEW -VMSize Standard_D2s_v3
$tsinfovmconfig = Set-AzVMOSDisk -VM $tsinfovmconfig -ManagedDiskId $newdisk.Id -CreateOption Attach -Windows
$publicip = New-AzPublicIpAddress -Name TSINFONEWVMIP -ResourceGroupName DemoVM -Location “East US” -AllocationMethod Dynamic

$myvnet = Get-AzVirtualNetwork -Name TSINFOVN1 -ResourceGroupName DemoVM

$mysubnet = Get-AzVirtualNetworkSubnetConfig -Name vmsubnet -VirtualNetwork $myvnet
$vmnic = New-AzNetworkInterface -Name “TSINFONEWVM_nic1” -ResourceGroupName DemoVM -Location “East US” -SubnetId $mysubnet.Id -PublicIpAddressId $publicip.Id

$tsinfovmconfig = Add-AzVMNetworkInterface -VM $tsinfovmconfig -Id $vmnic.Id
New-AzVM -VM $tsinfovmconfig -ResourceGroupName DemoVM -Location “East US”

Once We ran the above script, you can see we got a Successful response, as shown below

azure create vm from snapshot powershell

Now, let’s connect to the newly created Azure VM and see if the folder and text file we created exist. You can see the files and folder in the new VM as expected.

how to create disk from snapshot in azure

Conclusion

This article discussed how to create a VM from a snapshot in Azure using PowerShell. Thanks for reading this article !!!