Dmitry Nikolaev -

Get started Bring yourself up to speed with our introductory content.

3 PowerCLI snapshot management cmdlets you can use

You can use PowerCLI to manage snapshots in vSphere. With just three cmdlets, you can create, revert to and remove snapshots to keep your environment running smoothly.

Although you shouldn't use snapshots as backups, snapshots do ensure that you can restore to a specific VM state in a short-term scenario. When you use PowerCLI, you can create snapshots for many VMs at once and easily revert to those snapshots to restore those VMs.

Virtualization changed server management in the data center and in the cloud, and snapshots are part of that change. In just a few seconds, you can take a snapshot of a VMware VM and then revert back to that snapshot whenever you need. Even though you shouldn't use a snapshot as your primary backup system -- they take up a lot of storage capacity, which can hurt performance, and are not a full copy of data, and so don't provide the complete picture in a recovery situation -- snapshots work well as a short-term backup method.

You can manage snapshots much more efficiently with PowerCLI than with a GUI. There are only three PowerCLI snapshot management cmdlets: Get-Snapshot, New-Snapshot and Remove-Snapshot, and even though its purpose isn't snapshot management, Set-VM also has some snapshot functionality. With these cmdlets under your belt, managing a large vSphere environment and its snapshots becomes much simpler.

Create a snapshot

To create a new snapshot, you can use the New-Snapshot cmdlet:

C:\> New-Snapshot -VM Test-1 -Name 'This is a test snapshot' -Description 'Testing this out' -Quiesce -Memory
Name                 Description           PowerState
----                 -----------           ----------
This is a test sn... Testing this out      PoweredOn

The name and description fields are straightforward. These strings denote what you want to name the snapshot and describe its function. The -VM parameter denotes the VM you want to take the snapshot of.

Memory and quiesce are less straightforward parameters. Memory means that the snapshot will preserve the memory. The quiesce parameter ensures the good state of the guest file system via VMware Tools.

You can also use PowerCLI for more advanced snapshot operations, such as taking a snapshot of any Windows Server 2016 VM. To do this, filter your request by using the Where-Object cmdlet and use the Guest property, which should include the OS that the guest runs on. This results in a snapshot of each VM with the name 2016 test:

C:\> Get-VM | Where-Object {$_.Guest -like "*Windows Server 2016*"}
Name                 PowerState Num CPUs MemoryGB
----                 ---------- -------- --------
Test-1               PoweredOn  2        8.000
Test-2               PoweredOn  2        4.000
Test-3               PoweredOn  2        4.000
Test-4               PoweredOn  4        16.000
Test-5               PoweredOn  2        4.000
Test-6               PoweredOn  2        4.000
Test-7               PoweredOn  8        8.000
C:\> Get-VM | Where-Object {$_.Guest -like "*Windows Server 2016*"} | New-Snapshot -Name '2016 test'

Revert to a snapshot

You can also use PowerCLI to revert a VM to a snapshot. You shouldn't have to do this regularly outside of testing purposes, but it's a useful skill to have.

To revert a VM to a snapshot, use the Set-VM cmdlet and specify the name of the snapshot you want to revert to in the -Snapshot parameter. You can also use the Get-Snapshot cmdlet.

C:\> Set-VM -VM Test-1 -Snapshot (Get-Snapshot -VM Test-1 -Name 'This is a test snapshot')
Reverting VM 'Test-1' to snapshot 'This is a test snapshot'.
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): a
Name                 PowerState Num CPUs MemoryGB
----                 ---------- -------- --------
Test-1               PoweredOn  2        8.000

In a few seconds, your VM should revert back to your snapshot.

Remove a snapshot

You generally shouldn't keep snapshots around for long. Taking a snapshot requires a certain amount of overhead, and the more changes that occur on a VM after the snapshot, the more work your system must do to revert a VM back to its original state.

In PowerCLI, you can use the Remove-Snapshot cmdlet. You can also pipe the -RemoveChildren parameter, which ensures that any child of a snapshot is also removed along with the snapshot itself. For example, if you want to remove all snapshots of your Test-1 VM, you can pipe Get-Snapshot into Remove-Snapshot:

C:\> Get-Snapshot -VM Test-1 | Remove-Snapshot -RemoveChildren
Are you sure you want to perform this action?
Performing the operation "Removing snapshot." on target "VirtualMachineSnapshot-snapshot-108187".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): a

Dig Deeper on Scripting administrative tasks