Using Virtual Machines in Azure from time-to-time you may want to have a copy of the current point-in-time of your Virtual Machine, rather than taking a full backup of this using Recovery Services, can you create a snapshot of the Virtual Machine disk(s) using an Azure Snapshot
Snapshots are in theory, a simple read-only copy of your Virtual Machines (VM) disks from a point-in-time. These are used as a type of backup that can be used quickly to restore your virtual machine back to the state of the time the snapshot was taken or can be used to create a new virtual machine to assist with further troubleshooting
I am going to show you how to snapshot a virtual machine and restore via PowerShell. Along with this, we will look at incremental snapshots in an additional blog post; this feature was recently made publicly available, more information here
Creating Virtual Machine disk snapshots using PowerShell
Enter Virtual Machine parameters you want to snapshot
# Get VM
$VmName = "tamops-vm"
$VmResourceGroup = "tamops-snapshots"
$vm = get-azvm -Name $VmName -ResourceGroupName $VmResourceGroup
Snapshot the OS disk first
#VM Snapshot
Write-Output "VM $($vm.name) OS Disk Snapshot Begin"
$snapshotdisk = $vm.StorageProfile
$OSDiskSnapshotConfig = New-AzSnapshotConfig -SourceUri $snapshotdisk.OsDisk.ManagedDisk.id -CreateOption Copy -Location eastus -OsType Windows
$snapshotNameOS = "$($snapshotdisk.OsDisk.Name)_snapshot_$(Get-Date -Format ddMMyy)"
# OS Disk Snapshot
try {
New-AzSnapshot -ResourceGroupName $VmResourceGroup -SnapshotName $snapshotNameOS -Snapshot $OSDiskSnapshotConfig -ErrorAction Stop
} catch {
$_
}
Write-Output "VM $($vm.name) OS Disk Snapshot End"
Output of successful OS Disk Snapshot
VM tamops-vm OS Disk Snapshot Begin
ResourceGroupName : tamops-snapshots
ManagedBy :
Sku : Microsoft.Azure.Management.Compute.Models.SnapshotSku
TimeCreated : 27/03/2020 16:49:17
OsType : Windows
HyperVGeneration : V1
CreationData : Microsoft.Azure.Management.Compute.Models.CreationData
DiskSizeGB : 127
EncryptionSettingsCollection :
ProvisioningState : Succeeded
Id : /subscriptions/xxxxxxxxxxxxx/resourceGroups/tamops-snapshots/providers/Microsoft.Compute/snapshots/tamops-vm_d
isk1_a538b6c5dd52462bb1f2c83b4bb9cad2_snapshot_270320
Name : tamops-vm_disk1_a538b6c5dd52462bb1f2c83b4bb9cad2_snapshot_270320
Type : Microsoft.Compute/snapshots
Location : eastus
Tags : {}
VM tamops-vm OS Disk Snapshot End

Snapshot of data disks (snapshot per disk created separately
# Data Disk Snapshots
Write-Output "VM $($vm.name) Data Disk Snapshots Begin"
$dataDisks = ($snapshotdisk.DataDisks).name
foreach ($datadisk in $datadisks) {
$dataDisk = Get-AzDisk -ResourceGroupName $vm.ResourceGroupName -DiskName $datadisk
Write-Output "VM $($vm.name) data Disk $($datadisk.Name) Snapshot Begin"
$DataDiskSnapshotConfig = New-AzSnapshotConfig -SourceUri $dataDisk.Id -CreateOption Copy -Location eastus
$snapshotNameData = "$($datadisk.name)_snapshot_$(Get-Date -Format ddMMyy)"
New-AzSnapshot -ResourceGroupName $VmResourceGroup -SnapshotName $snapshotNameData -Snapshot $DataDiskSnapshotConfig -ErrorAction Stop
Write-Output "VM $($vm.name) data Disk $($datadisk.Name) Snapshot End"
}
Write-Output "VM $($vm.name) Data Disk Snapshots End"
Output of successful data disk snapshots
VM tamops-vm Data Disk Snapshots Begin
VM tamops-vm data Disk datadisk1 Snapshot Begin
ResourceGroupName : tamops-snapshots
ManagedBy :
Sku : Microsoft.Azure.Management.Compute.Models.SnapshotSku
TimeCreated : 27/03/2020 17:03:24
OsType :
HyperVGeneration :
CreationData : Microsoft.Azure.Management.Compute.Models.CreationData
DiskSizeGB : 32
EncryptionSettingsCollection :
ProvisioningState : Succeeded
Id : /subscriptions/xxxx-xxx-xxxxxxxxx/resourceGroups/tamops-snapshots/providers/Microsoft.Compute/snapshots/datadisk1_s
napshot_270320
Name : datadisk1_snapshot_270320
Type : Microsoft.Compute/snapshots
Location : eastus
Tags : {}
VM tamops-vm data Disk datadisk1 Snapshot End
VM tamops-vm data Disk datadisk2 Snapshot Begin
ResourceGroupName : tamops-snapshots
ManagedBy :
Sku : Microsoft.Azure.Management.Compute.Models.SnapshotSku
TimeCreated : 27/03/2020 17:03:32
OsType :
HyperVGeneration :
CreationData : Microsoft.Azure.Management.Compute.Models.CreationData
DiskSizeGB : 32
EncryptionSettingsCollection :
ProvisioningState : Succeeded
Id : /subscriptions/xxxx-xxx-xxxxxxxxx/resourceGroups/tamops-snapshots/providers/Microsoft.Compute/snapshots/datadisk2_s
napshot_270320
Name : datadisk2_snapshot_270320
Type : Microsoft.Compute/snapshots
Location : eastus
Tags : {}
VM tamops-vm data Disk datadisk2 Snapshot End
VM tamops-vm Data Disk Snapshots End

Full script
# Get VM
$VmName = "tamops-vm"
$VmResourceGroup = "tamops-snapshots"
$vm = get-azvm -Name $VmName -ResourceGroupName $VmResourceGroup
#VM Snapshot
Write-Output "VM $($vm.name) OS Disk Snapshot Begin"
$snapshotdisk = $vm.StorageProfile
$OSDiskSnapshotConfig = New-AzSnapshotConfig -SourceUri $snapshotdisk.OsDisk.ManagedDisk.id -CreateOption Copy -Location eastus -OsType Windows
$snapshotNameOS = "$($snapshotdisk.OsDisk.Name)_snapshot_$(Get-Date -Format ddMMyy)"
# OS Disk Snapshot
try {
New-AzSnapshot -ResourceGroupName $VmResourceGroup -SnapshotName $snapshotNameOS -Snapshot $OSDiskSnapshotConfig -ErrorAction Stop
} catch {
$_
}
Write-Output "VM $($vm.name) OS Disk Snapshot End"
# Data Disk Snapshots
Write-Output "VM $($vm.name) Data Disk Snapshots Begin"
$dataDisks = ($snapshotdisk.DataDisks).name
foreach ($datadisk in $datadisks) {
$dataDisk = Get-AzDisk -ResourceGroupName $vm.ResourceGroupName -DiskName $datadisk
Write-Output "VM $($vm.name) data Disk $($datadisk.Name) Snapshot Begin"
$DataDiskSnapshotConfig = New-AzSnapshotConfig -SourceUri $dataDisk.Id -CreateOption Copy -Location eastus
$snapshotNameData = "$($datadisk.name)_snapshot_$(Get-Date -Format ddMMyy)"
New-AzSnapshot -ResourceGroupName $VmResourceGroup -SnapshotName $snapshotNameData -Snapshot $DataDiskSnapshotConfig -ErrorAction Stop
Write-Output "VM $($vm.name) data Disk $($datadisk.Name) Snapshot End"
}
Write-Output "VM $($vm.name) Data Disk Snapshots End"
Restoring Virtual Machine using snapshot
If you ran the above script, you will have three snapshots as below

Create OS Disk from Snapshot
# Create OS Disk from snapshot
$SnapshotName = "tamops-vm_disk1_a538b6c5dd52462bb1f2c83b4bb9cad2_snapshot_270320"
$SnapshotResourceGroup = "tamops-snapshots"
$DiskNameOS = "tamops-vm-snapshotdisk"
$snapshotinfo = Get-AzSnapshot -ResourceGroupName $SnapshotResourceGroup -SnapshotName $snapshotName
New-AzDisk -DiskName $DiskNameOS (New-AzDiskConfig -Location eastus -CreateOption Copy -SourceResourceId $snapshotinfo.Id) -ResourceGroupName $SnapshotResourceGroup
Output of successful OS disk creation from snapshot
ResourceGroupName : tamops-snapshots
ManagedBy :
Sku : Microsoft.Azure.Management.Compute.Models.DiskSku
Zones :
TimeCreated : 27/03/2020 17:52:23
OsType : Windows
HyperVGeneration : V1
CreationData : Microsoft.Azure.Management.Compute.Models.CreationData
DiskSizeGB : 127
EncryptionSettingsCollection :
ProvisioningState : Succeeded
DiskIOPSReadWrite : 500
DiskMBpsReadWrite : 60
DiskState : Unattached
Id : /subscriptions/xxxx-xxx-xxxxxxxxx/resourceGroups/tamops-snapshots/providers/Microsoft.Compute/disks/tamops-vm-snaps
hotdisk
Name : tamops-vm-snapshotdisk
Type : Microsoft.Compute/disks
Location : eastus
Tags : {}

Attach new disk now to VM, in the VM disk settings – select Swap OS Disk

Attaching new data disk snapshots, can be done via PowerShell but depending on scenario it may be as easy to just attach as new data disks, done below, or alternatively re-run the above script with the parameters for each data disk

Hopefully you find this blog useful and will assist you in relation to Virtual Machine snapshots in Azure!
If I’m not mistaken, this will only work for windows VM. You could use the parameter -OsType $vm.StorageProfile.OsDisk.OsType when defining New-AzSnapshotConfig to make the snapshots work on any VM.
Hi Enrico that is correct, I created the PowerShell for a Windows VM
Hello Thomas,
Nice article. Quick question (could be a dumb one). This PS script creates a duplicate of the original machine while the original is still running. Won’t this be a problem in terms of license, machine id etc.?
Hi Vysask,
The example I show justswaps the OS disk. No issue really (depending on VM settings)
If you were creating a new VM from the snapshot while keeping the original VM as well. Then you’d consider it like a VM backup and restore. The same OS related considerations would apply
Can we have a scrip to take OD disk snapshot for multiple servers in one go
Hi,
Not sure what you mean?
You mean to loop over multiple computers? you could just add a foreach loop
Thanks
Thanks for your help!
HTH someone
https://stackoverflow.com/questions/46811062/how-to-revert-an-azure-disk-back-to-its-former-snapshot/66502014#66502014
Thank you, glad the blog post helped 👍
Two years after you posted this and still very helpful, if you have more than one data disk is there an easy way to loop back through on the recreate script?
I’m having to ‘move’ servers from a region to a zone in the region and was working on automating it and stumbled across this. Helped me snapshot much faster, but trying reverse the snapshot for the data disks so that it takes all the ‘data disk’ snaps in my resource group and makes the disk image out of each one.
Hi Tom,
Cheers for the comment!
Regarding your query, you should be able to query all disk images in a resource group and then create images with each one? I do something similar here: https://thomasthornton.cloud/2020/04/06/copy-azure-virtual-machine-snapshots-to-another-region-and-create-managed-disks-using-powershell/
see
$snapshots = Get-AzSnapshot -ResourceGroupName $snapshotresourcegroup | ?{($_.TimeCreated) -gt ([datetime]::UtcNow.Addhours(-12))}
Thanks
Thomas