How To Create Azure Windows Virtual Machine From A Snapshot PowerShell

How To Create Azure Windows Virtual Machine From A Snapshot PowerShell

In this Azure tutorial, we will discuss How To Create Azure Windows Virtual Machine From A Snapshot, Along with this, we will also discuss a few other topics like What is Azure Virtual Machine?, What is VM snapshots?, Create Azure Windows Virtual Machine Using PowerShell, Create A Virtual Machine From A Snapshot Azure Portal, etc.

How To Create Azure Windows Virtual Machine From A Snapshot PowerShell? We will create the Azure Virtual Machine from a snapshot with the below steps

  1. The first step is to create a new resource group / You can also use your existing resource group as well
  2. Creating a new virtual network as the second step.
  3. Creating an Azure Virtual machine.
  4. Creating two files in the newly created Azure Virtual Machine.
  5. Creating the Snapshot.
  6. Using the Snapshot, we will create a new Virtual machine.
  7. Verification of the two files that we have created in the New Azure Virtual Machine.

How To Create Azure Windows Virtual Machine From A Snapshot PowerShell

Well, here we will discuss a very usual topic, How To Create Azure Windows Virtual Machine From A Snapshot using PowerShell. Before starting the actual topic, we will discuss a bit on Azure Virtual Machine and the Prerequisites needed here.

What is Azure Virtual Machine?

Azure Virtual Machine is a vital part of hardware visualization. These machines are pre-made systems designed on the basis of the visualization concepts. The concept behind Azure Virtual Machine is the same as cloud platform-visualization. One of the main benefits of using Azure VM or Azure Virtual Machine is that the users can open and shut down whenever they like. 

What is VM snapshots?

Virtual machine snapshots basically help to preserve the data and state of a virtual machine at some point in time. You can also consider that a VM snapshot is the copy of the disk file of your Virtual machine. VM snapshots are very helpful to immediately recover a virtual machine from a disaster.

Consider there is a scenario where you want to get certain data from the snapshot without restoring the complete VM. In that case, one of the excellent ways is to create a VM from the snapshot and get the specific data that you need.

Assuming, you have a little bit of an idea now about Azure Virtual Machine and VM snapshots, let’s discuss the Prerequisites needed to Create Azure Windows Virtual Machine From A Snapshot using PowerShell.

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.

Create Azure Windows Virtual Machine Using PowerShell

Ohk, lets create a new Azure VM using PowerShell using the below steps.

Connect To Azure

Open the PowerShell ISE as Run as Administrator mode and then execute the below PowerShell cmdlet to connect to Azure using Connect-AzAccount PowerShell cmdlet.

PS C:\WINDOWS\system32> Connect-AzAccount

Once, you will execute the above PowerShell cmdlet, it will prompt you to Provide your Azure Credentials. Make sure to login with your Global Administrator credentials.

Create a new resource group

In order to create an Azure Virtual machine, as a first step, we need to create a resource group using PowerShell. Use the below PowerShell script to create a resource group using PowerShell.

PS C:\WINDOWS\system32> New-AzResourceGroup -Name DemoVM -Location "East US"

Once, we have executed the above PowerShell cmdlet, you can able to see that the Resource group has been successfully created without any issue.

DemoVM is the name of our resource group and The region for the resource group is East US.

Step-by-Step Guide: Create Azure Windows Virtual Machine from a Snapshot (PowerShell Guide)

Create a new virtual network

Now, our resource group is ready and as a next step we need to create a new virtual network using PowerShell. Use the below PowerShell cmdlet to create a new virtual network using PowerShell.

$vmsnet = New-AzVirtualNetworkSubnetConfig -Name vmsubnet -AddressPrefix “10.0.2.0/24”

New-AzVirtualNetwork -Name TSINFOVN1 -ResourceGroupName DemoVM -Location “East US” -AddressPrefix “10.0.0.0/16” -Subnet $vmsnet

Once, we have executed the above PowerShell script, you can able to see, the virtual network has been successfully created without any issue

How do I create a VM on Azure snapshot?
Name                   : TSINFOVN1
ResourceGroupName      : DemoVM
Location               : eastus
Id                     : /subscriptions/1cdf###-dee5-###-9c9c-feaa72a5cbd1/resourceG
                         roups/DemoVM/providers/Microsoft.Network/virtualNetworks/TSIN
                         FOVN1
Etag                   : W/"8b5818b8-231f-4102-a58c-d4059303c4d6"
ResourceGuid           : 16a24415-7405-4a35-9d00-0920e25bd07e
ProvisioningState      : Succeeded
Tags                   : 
AddressSpace           : {
                           "AddressPrefixes": [
                             "10.0.0.0/16"
                           ]
                         }
DhcpOptions            : {}
Subnets                : [
                           {
                             "Delegations": [],
                             "Name": "vmsubnet",
                             "Etag": "W/\"8b5818b8-231f-4102-a58c-d4059303c4d6\"",
                             "Id": "/subscriptions/1cdf4300-dee5-4518-9c9c-feaa72a5cbd
                         1/resourceGroups/DemoVM/providers/Microsoft.Network/virtualNe
                         tworks/TSINFOVN1/subnets/vmsubnet",
                             "AddressPrefix": [
                               "10.0.2.0/24"
                             ],
                             "IpConfigurations": [],
                             "ServiceAssociationLinks": [],
                             "ResourceNavigationLinks": [],
                             "ServiceEndpoints": [],
                             "ServiceEndpointPolicies": [],
                             "PrivateEndpoints": [],
                             "ProvisioningState": "Succeeded",
                             "PrivateEndpointNetworkPolicies": "Enabled",
                             "PrivateLinkServiceNetworkPolicies": "Enabled",
                             "IpAllocations": []
                           }
                         ]
VirtualNetworkPeerings : []
EnableDdosProtection   : false
DdosProtectionPlan     : null

Till Now, we have successfully created the resource group and the virtual network. Now, the time to create a new Azure VM.

Creating Azure VM PowerShell

Use the below PowerShell cmdlet to create an Azure Virtual machine.

$loginDetails = Get-Credential

New-AzVm -ResourceGroupName DemoVM -Name “TSINFOVM” -Location “East US” -VirtualNetworkName “TSINFOVN1” -SubnetName “vmsubnet” -addressprefix 10.0.2.0/24 -PublicIpAddressName “TSINFOIP1” -OpenPorts 3389 -Image win2019datacenter -Size Standard_D2s_v3 -Credential $loginDetails

Once we have executed the above PowerShell script, you can able to see the Azure Virtual machine has been created successfully without any issue

Can you create a VM from a snapshot?
Easy Azure VM Snapshots with PowerShell

The output looks like below

ResourceGroupName        : DemoVM
Id                       : /subscriptions/1cdf4300-dee5-4518-9c9c-feaa72a5cbd1/resourc
eGroups/DemoVM/providers/Microsoft.Compute/virtualMachines/TSINFOVM
VmId                     : b90fd30b-cefd-432c-a06f-81598e6dd91d
Name                     : TSINFOVM
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 : tsinfovm-a5a358.East US.cloudapp.azure.com

Just to make sure that the virtual machine has been created successfully, Let’s login to the Azure Portal and check that the VM got created successfully. You can able to see below that the Azure VM got created successfully without any issue.

How do I clone an azure Windows virtual machine

Now let’s login to the Azure virtual machine that we have created and will create two text files and will keep them on the Desktop.

The quickest way to create new VMs in Azure from existing VM snapshots

So now, Our Azure Virtual machine is ready with the test files. Now the next step is to create the snapshot of the Azure Virtual Machine.

Create the Snapshot of the VM

You can use the below PowerShell script to create the snapshot of the Azure Virtual machine.

$myvm = Get-Azvm -ResourceGroupName DemoVM -Name TSINFOVM
$snapconfDetails = New-AzSnapshotConfig -SourceUri $myvm.StorageProfile.OsDisk.ManagedDisk.Id -Location “East US” -CreateOption copy
New-AzSnapshot -Snapshot $snapconfDetails -SnapshotName tsinfosnap -ResourceGroupName DemoVM

Once, we have executed the above script, you can able to see the snapshot of the Azure VM has been created successfully

Using PowerShell to Create Virtual Machine Snapshots
azure clone vm

The output looks like below

ResourceGroupName            : DemoVM
ManagedBy                    : 
Sku                          : Microsoft.Azure.Management.Compute.Models.SnapshotSku
TimeCreated                  : 12/10/2020 12:53:26 AM
OsType                       : Windows
HyperVGeneration             : 
CreationData                 : Microsoft.Azure.Management.Compute.Models.CreationData
DiskSizeGB                   : 127
DiskSizeBytes                : 136367308800
UniqueId                     : 25f77a5b-0bb9-4b04-955e-016da959d351
EncryptionSettingsCollection : 
ProvisioningState            : Succeeded
Incremental                  : False
Encryption                   : Microsoft.Azure.Management.Compute.Models.Encryption
Id                           : /subscriptions/1cdf4300-dee5-4518-9c9c-feaa72a5cbd1/res
                               ourceGroups/DemoVM/providers/Microsoft.Compute/snapshot
                               s/tsinfosnap
Name                         : tsinfosnap
Type                         : Microsoft.Compute/snapshots
Location                     : eastus
Tags                         : {}
NetworkAccessPolicy          : AllowAll
DiskAccessId                 : 

Now the next step is to Create Azure windows virtual machine from snapshot.

Create Azure Windows Virtual Machine From Snapshot

Well, we have created the Azure Windows Virtual Machine Snapshot above, Now We will be Creating 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

Now, we will have to 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

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

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

Now, 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 able to see we got the 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 have created is there or not. You can able to see the files and folder is there in the new VM as expected.

how to create disk from snapshot in azure
azure clone vm from snapshot

So, this is How To Create Azure Windows Virtual Machine From Snapshot.

Create A Virtual Machine From A Snapshot Azure Portal

Well, above we have discussed How To Create Azure Windows Virtual Machine From Snapshot PowerShell. Now let’s discuss How to Create A Virtual Machine From A Snapshot Azure Portal. Follow the below steps to Create A Virtual Machine From A Snapshot using Azure Portal.

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

Step-2: Navigate to the respective virtual machine for the one you want to create the snapshot. Then click on the Disks link from the left navigation. Now click on the Disk name under the OS disk section as highlighted below.

How to create a VM from a snapshot in Azure

Step-3: Now, click on the + Create snapshot button on the Disk page as shown below.

How to create a VM from a snapshot in Azure Portal

Step-4: Fill out the below details as shown here

  • Resource group: You can select the existing resource group or if you don’t want to use the existing resource group, you click on the Create new link to create a new resource group.
  • Name: Provide a unique name for the snapshot.
  • Snapshot type: Select the snapshot type as per your requirement.
  • Storage type: Select the storage type based on your requirement.

Once you have provided the above details, click on the Review + Create button.

Create A Virtual Machine From A Snapshot using Azure Portal

Now, it will validate all the details provided by you and will show you Validation Passed, once all the details are correct. then you can able to see the Create button is enabled. Click on the Create button to create the Snapshot for the Virtual machine.

You can able to see that the deployment is completed successfully without any issue. Click on the Go to resource button to navigate to the Snapshot that you have created just now.

azure create disk from snapshot

Now our VM snapshot is ready to be used. Let’s see how to create a VM with this snapshot. Now as the next step we need to create a managed disk for the new Virtual Machine.

Step-5: Search for the Disks on the Global search bar in the Azure Portal and click on the search result Disks as highlighted below.

Create Azure VM from Snapshot – PowerShell

Step-6: On the Disks Page, click on the +Add button to create a new managed Disk. Now on the Managed disk page, Provide the below details.

  • Subscription: Select the correct subscription that you want to use here to create the managed disk.
  • Resource group: Either you can choose your Existing resource group or you can also create anew one.
  • Disk Name: Provide a unique name for the disk.
  • Region: Make sure to select the region your snapshot was saved in, otherwise won’t able to see your snapshot.
  • Size: Now click on the change size link to select the size based on your requirement and usages.
  • Source type: Choose the source type option as the snapshot.
  • Source Snapshot: Make sure to choose the snapshot that you have created above.

Finally, click on the Review + Create button as highlighted below.

Create Azure VM from Snapshot using PowerShell

Once, You will click on the Review + Create button, it will validate all the details provided by you and will show you Validation Passed, once all the details are correct. then you can able to see the Create button is enabled. Click on the Create button to create the new managed disk.

Now you can able to see that the deployment is completed successfully without any issue. Click on the Go-To Resource button to navigate to the newly created managed Disk.

Create and restore Azure virtual machine snapshots

The next step is, on the Disk page, click on the + Create VM button to create the new Virtual Machine as highlighted below.

Creating And Restoring Azure Virtual Machine Snapshots For Managed Disks

Now, Fill the below details on the Create a virtual machine page as highlighted below

  • Resource Group: Provide the Resource Group Details here. You can also create a new Resource group by clicking on the Create new link.
  • Virtual Machine Name: Provide a unique name for your Virtual Machine.
  • Image: Make sure to choose the proper image option.
  • Size: Select the size for your Virtual machine based on your requirement.
  • Public Inbound Ports: Select the Public Inbound Ports based on your requirement.
  • Select Inbound Ports: Provide the proper Select Inbound Ports as mentioned below.
  • License type: Select the Proper License type based on yours.
  • Would you like to use an existing Windows server License?: Select the correct choice based on your need.

Finally, click on the Review + Create button.

azure create image from snapshot
create vm from snapshot azure Portal

After validating all the details, you can able to see the Create button. Click on the Create button to create the Virtual Machine from the snapshot. After a few seconds, you can able to see the Deployment is completed successfully. Click on the Go to Resource button to see the newly created Virtual machine.

You may also like the following article

Wrapping Up

Well, In this article, we have discussed How To Create Azure Windows Virtual Machine From A Snapshot PowerShell?, What is Azure Virtual Machine?, What is VM snapshots? and Create Azure Windows Virtual Machine Using PowerShell and finally, we have discussed Create A Virtual Machine From A Snapshot Azure Portal.