Sashkin - Fotolia

Tip

Manage all your Hyper-V snapshots with PowerShell

Using PowerShell commands to take and remove Hyper-V snapshots, and to restore VMs to a previous state is easy and efficient, especially when working with bulk VMs.

It's much easier to manage Hyper-V snapshots using PowerShell than a GUI because PowerShell offers the greater flexibility. Once you're familiar with the basic commands, you'll be equipped to oversee and change the state of the VMs in your virtual environment.

PowerShell not only reduces the time it takes to perform a task using a GUI tool, but it also reduces the time it takes to perform repeated tasks. For example, if you want to see the memory configured on all Hyper-V VMs, a quick PowerShell command or script is easier to execute than checking VMs one by one. Similarly, you can perform operations related to Hyper-V snapshots using PowerShell.

A snapshot -- or checkpoint, depending on which version of Windows Server you have -- is a point-in-time picture of a VM that you can use to restore that VM to the state it was in when the snapshot was taken. For example, if you face issues when updating Windows VMs and they don't restart properly, you can restore VMs to the state they were in before you installed the updates.

Similarly, developers can use checkpoints to quickly perform application tests.

Before Windows Server 2012 R2, Microsoft didn't support snapshots for production use. But starting with Windows Server 2012 R2, snapshots have been renamed checkpoints and are well-supported in a production environment.

PowerShell commands for Hyper-V snapshots and checkpoints

Microsoft offers a few PowerShell commands to work with Hyper-V checkpoints and snapshots, such as Checkpoint-VM, Get-VMSnapshot, Remove-VMSnapshot and Restore-VMSnapshot

If you want to retrieve all the Hyper-V snapshots associated with a particular VM, all you need to do is execute the Get-VMSnapshot -VMName <VMNameHere> PowerShell command. For example, the PowerShell command below lists all the snapshots associated with SQLVM:

Get-VMSnapshot -VMName SQLVM

There are two types of Hyper-V checkpoints available: standard and production checkpoints. If you just need all the production checkpoints for a VM, execute the PowerShell command below:

Get-VMSnapshot -VMName SQLVM -SnapshotType Production

To list only the standard checkpoints, execute the following PowerShell command:

Get-VMSnapshot -VMName SQLVM -SnapshotType Standard

When it comes to creating Hyper-V checkpoints for VMs, use the Checkpoint-VM PowerShell command. For example, to take a checkpoint for a particular VM, execute the command below:

Checkpoint-VM -Name TestVM -SnapshotName TestVMSnapshot1

The above command creates a checkpoint for TestVM on the local Hyper-V server, but you can use the following command to create a checkpoint for a VM located on a remote Hyper-V server:

Get-VM SQLVM -ComputerName HyperVServer | Checkpoint-VM

There are situations where you might want to create Hyper-V checkpoints of VMs in bulk. For example, before installing an update on production VMs or upgrading line-of-business applications in a VM, you might want to create checkpoints to ensure you can successfully restore VMs to ensure business continuity. But if you have several VMs, the checkpoint process might take a considerable amount of time.

You can design a small PowerShell script to take Hyper-V checkpoints for VMs specified in a text file, as shown in the PowerShell script below:

$ProdVMs = "C:\Temp\ProdVMs.TXT"
Foreach ($ThisVM in Get-Content $ProdVMs)
{   
$ChkName = $ThisVM+"_BeforeUpdates"
Checkpoint-VM -Name $ThisVM -SnapshotName $ChkName
}
Write-Host "Script finished creating Checkpoints for Virtual Machines."

The above PowerShell script gets VM names from the C:\Temp\ProdVMs.TXT file one by one and then runs the Checkpoint-VM PowerShell command to create the checkpoints.

To remove Hyper-V snapshots from VMs, use the Remove-VMSnapshot PowerShell command. For example, to remove a snapshot called TestSnapshot from a VM, execute the following PowerShell command:

Get-VM SQLVM | Remove-VMSnapshot -Name TestSnapshot

To remove Hyper-V checkpoints from bulk VMs, use the same PowerShell script you used to create the checkpoints. Let's assume all the VMs are working as expected after installing the updates and you would like to remove the checkpoints. Simply execute the PowerShell script below:

$ProdVMs = "C:\Temp\ProdVMs.TXT"
Foreach ($ThisVM in Get-Content $ProdVMs)
{   
$ChkName = $ThisVM+"_BeforeUpdates"
Get-VM $ThisVM | Remove-VMSnapshot $ChkName
}
Write-Host "Script finished removing Checkpoints for Virtual Machines."

To remove a specific snapshot associated with all your VMs, use the following PowerShell command:

Get-VMSnapShot -Name BeforeUpdate | Remove-VMSnapShot

As you can see in Figure A below, there are two VMs that the BeforeUpdate snapshot needs to be removed from.

Remove a snapshot
Figure A. Remove a specific snapshot from all your VMs.

To restore Hyper-V snapshots for VMs, use the Restore-VMSnapshot PowerShell cmdlet. For example, to restore or apply a snapshot to a particular VM, use the following PowerShell command:

Restore-VMSnapshot -Name "TestSnapshot1" -VMName SQLVM -Confirm:$False

Let's assume your production VMs aren't starting up after installing the updates and you would like to restore the VMs to their previous states. Use the PowerShell script below and perform the restore operation:

$ProdVMs = "C:\Temp\ProdVMs.TXT"
Foreach ($ThisVM in Get-Content $ProdVMs)
{   
$ChkName = $ThisVM+"_BeforeUpdates"
Restore-VMSnapshot -Name $ChkName -VMName $ThisVM -Confirm:$False
}
Write-Host "Script finished Restoring Checkpoints for Virtual Machines."

Note that, by default, when restoring a checkpoint for a VM, the command asks for confirmation. To avoid the confirmation prompt, add the Confirm:$False parameter to the command, as shown above.

Dig Deeper on IT systems management and monitoring

Software Quality
App Architecture
Cloud Computing
SearchAWS
TheServerSide.com
Data Center
Close