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.
Table of Contents
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.

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 tsinfosnapCreating 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 tsinfoDiskInitializing 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 -WindowsCreating 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 DynamicCreating 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.IdCreating 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.IdNew-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

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.

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

I am Rajkishore, and I am a Microsoft Certified IT Consultant. I have over 14 years of experience in Microsoft Azure and AWS, with good experience in Azure Functions, Storage, Virtual Machines, Logic Apps, PowerShell Commands, CLI Commands, Machine Learning, AI, Azure Cognitive Services, DevOps, etc. Not only that, I do have good real-time experience in designing and developing cloud-native data integrations on Azure or AWS, etc. I hope you will learn from these practical Azure tutorials. Read more.
