Using PowerShell to Create Virtual Machine Snapshots in VMware

Hi All,

VMware snapshots can be a very good insurance policy when making changes to a VM. In this blog I will explain how to use PowerShell to create VM snapshots in VMWare. We will be using the “Get-VM” cmdlet and piping it to the “New-Snapshot” to accomplish this in the examples below. To do anything with PowerShell in VMware you will need to install VMware vSphere PowerCLI. You will then need to open PowerCLI connect to your VMWare vCenter server by typing “Connect-VIServer vCenterServerName”.

First I will start with taking a snapshot of a single virtual machine. In this example we will simply use the “Get-VM” cmdlet and specify the name of the virtual machine. We then pipe it the “New-Snapshot” cmdlet. I have added in the “Memory” and “Quiesce” parameters to snap the memory and quiesce the files. The “Name” parameter is used to specify the name of the snapshot. In the command below we will be calling the snapshot name “PrePatch”. Replace VMname with the name of your VM and PrePatch with the name you would like to call the snapshot.

Get-VM VMname | New-Snapshot -Memory -Quiesce -Name PrePatch

Once the command completes (time will vary depending on the size of your VM), you will have a snapshot of the virtual machine with the name you specified. For information viewing VM snapshots please see my other articleUsing PowerShell to get a list Virtual Machine Snapshots in VMware ESXi 4.1

Now that we know how to take a snapshot of one VM, let’s apply this to taking snapshots of multiple virtual machines. If you have your VMs organized by folder in vCenter, you can easily take a snapshot of all VMs in the folder by specifying the location. In the example below, we take a snapshots of all virtual machines located under the “My Lab” folder.

get-vm -location “My Lab” | New-Snapshot -Memory -Quiesce -Name PrePatch

When the command above completes you will have a snapshot for each VM located in the “My Lab” folder. Each Snapshot will be named “Prepatch”.

IMPORTANT NOTE: You should be careful with the amount of Virtual Machines you snap at the same time. Depending on your environment, taking too many snapshots at one time could wreak havoc on your storage or on your environment in general. It would be best to test this out on a limited number of VMs in your environment or in a lab first.

Its also not a good idea to leave snapshots sitting around for too long. For information about removing virtual machine snapshots using PowerShell please see my other article Using PowerShell to Remove Virtual Machine Snapshots in VMware ESXi 4.1.

I hope this helps. If you have any questions or feedback, please leave a comment.

Related Links:

Using PowerShell to get a list Virtual Machine Snapshots in VMware ESXi 4.1

Using PowerShell to Remove Virtual Machine Snapshots in VMware ESXi 4.1

Using PowerShell to create a Virtual Machine Inventory in VMware and Export it to a CSV File

Using PowerShell to View  Virtual Machine Disks (VMDK) Information in VMware and Export to a CSV File

 

Leave a Reply

Your email address will not be published. Required fields are marked *