As many who use vCenter to host their virtual servers know, snapshots are a critical function, one that is frequently undertaken before upgrades or changes to a system. However, taking snapshots can be a tedious process, particularly if you are taking multiple snapshots. Fortunately, VMware has created the PowerCLI PowerShell module, which can make this process much more efficient. I used the VMware.VimAutomation.Core PowerShell module, which was downloaded from the PowerShell Gallery website. Let's review how to perform common tasks in the web view, and see how to do the same task from the CLI.

Connection to vSphere

Using the GUI, a user goes to a URL, and enters their credentials. If your environment has multiple vSphere servers, the user will need to open multiple tabs and connect to each server separately.

Vsphere web console login

Vsphere web console login

You can also use PowerShell:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Force
Import-Module VMware.VimAutomation.Core -Force
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
$Credential = Import-CliXml -path <scrubbed> 
# Credentials
$vcserver = "vcenterservername"
$vcusername = $Credential.UserName
# Login VMware
Connect-VIServer -Server $vcserver -Protocol https -User $vcusername -Password $Credential.GetNetworkCredential().Password

The saved credentials method I am using only works for my credentials when I am logged into that particular computer. This allows for added security, as no one else can copy my saved credentials and use them on another server.

The Connect-VIServer cmdlet allows me to connect to the vSphere server; if I want to connect to multiple servers using the same credentials, I can simply add the additional server names to the line "$vcserver = "vcenterservername". If different credentials are needed, I can use several Connect-VIServer commands with the username and password needed for each vSphere server. The primary advantage of doing this is that it allows the user to access all the vSphere servers from a single PowerShell session.

An example of connecting to multiple vCenter Servers at one time is as follows:

$vCenters = "vCenter01-EAST",”vCenter02-WEST”
Connect-VIServer -Server $vCenters -Protocol https -User $vcusername -Password $Credential.GetNetworkCredential().Password

You can see which vCenter Servers you are connected to using the global variable $global:defaultviservers:


Name                           Port   User
----                           ----   ----
vCenter01-EAST                 443    DOMAIN\UserName
vCenter02-WEST                 443    DOMAIN\UserName

Taking snapshots

To take a snapshot from the web interface, you need to go to each individual server and manually initiate the snapshot process. Below are some screenshots of the steps that you need to complete.

Take a snapshot in the GUI

Take a snapshot in the GUI

Naming the snapshot

Naming the snapshot

To take a snapshot in the GUI, find the VM machine you want to snapshot, then select Actions > Snapshots > Take Snapshot.

These steps need to be repeated for every server that you want to take a snapshot of.

Using the PowerCLI module allows for a more streamlined process, especially if you are taking snapshots of multiple servers. The first step is to create an array of all the servers that you want to take a snapshot of, and then save it as a variable. Then, use a ForEach loop to dynamically create the SnapShotName (in my example, I simply appended the phrase "-backup" to the VM name, but you can add a date or any other character if you would prefer), write a status message to the PowerShell console, and use the New-Snapshot cmdlet to create the snapshot. I also statically define each server as an individual string, and then add each new server name to the array $AppVMs.

# Take Snapshot
ForEach ($VM in  $AppVMs){
        $snapshotname = $vm + "-backup"
        write-host "Creating snapshot [$snapshotname] for the VM [$vm]"
        New-Snapshot -vm $vm -name $snapshotname -confirm:$false -runasync:$true

Statically defining each VM is useful if the VM names are all different; however, if the names are similar, you can utilize an alternative method for obtaining the VM names:

$AppVMs= Get-VM -Name server*

Using this method creates an array of all the VMs that start with the word "server," including the four used in the previous example. If I specifically want to use only those four servers, I can use this method:

$AppVMs= Get-VM -Name "server[1-4]"

This allows me to explicitly get the VMs of just those servers. If there are 100 servers, and I only want to work with the middle 50, I can do this:

$AppVMs= Get-VM -Name "server[26-75]"

Because I am storing the entire VM object in the $AppVMs array, I need to change my snapshot script to use the .Name property from each VM instead of the statically defined string, as was used in the previous snippet.

It looks like this:

# Take Snapshot
ForEach ($VM in  $AppVMs){
        $snapshotname = $vm.Name + "-backup"
"Creating snapshot [$snapshotname] for the VM [$($vm.Name)]"
        New-Snapshot -vm $vm -name $snapshotname -confirm:$false -runasync:$true

Keep in mind that you need the virtual machine name, which may or may not be the server's host name or DNS name. If someone changes the VM name, you will need to update your PowerShell command.

Removing snapshots

Once you are done making your changes to a server (or multiple servers), you will need to remove the snapshots because they can take up valuable space on your vCenter Server. Not only that, but the longer the snapshots exist, the higher the probability that you will encounter issues when you try to remove them.

In the web console, go to each virtual machine and click Actions > Snapshots > Manage Snapshots.

Managing snapshots

Managing snapshots

Any snapshots that you have saved will be visible on the Manage Snapshots screen.

Delete or revert to a previously saved snapshot

Delete or revert to a previously saved snapshot

From the Manage Snapshots screen, you can either delete or revert to a previously saved snapshot.

As with taking a snapshot, we need to do this for every virtual machine.

In PowerShell, we can use a snippet similar to the New-Snapshot cmdlet:

ForEach ($VM in $AppVMs){
    $snapshotname = $vm + "-backup"
    $snap = Get-Snapshot -vm $vm -name $snapshotname
    write-host "Removing snapshot [$snapshotname] for the VM [$vm]"
    remove-snapshot -snapshot $snap -confirm:$false -runasync:$true

In this snippet, we need to retrieve the individual snapshot using the Get-Snapshot cmdlet and then remove it using remove-snapshot.

Snapshot cleanup methods

As mentioned earlier, keeping too many snapshots can cause issues within vCenter. During your career, you will likely experience an instance where another user fails to remove their snapshots; it can be beneficial to proactively view how many snapshots are currently saved on the server.

Let's start with the GUI. Unfortunately, it isn't possible to create a nice display or overview of every snapshot listed for every VM. vSphere just doesn't have the ability to do that for you, no matter how convenient it would be for you.

However, with PowerCLI, you can create a report using the following PowerShell line:

Get-VM | Get-Snapshot | select VM, Name, Created | Export-Csv .\VM.csv

This single line will query all VMs in every vSphere Server that you are connected to and retrieve all the associated snapshots. It then selects the VM name, the snapshot name, and the creation date, and exports that information to a CSV file. This lets the VM admins decide what to do about the snapshots. If the decision is to remove all snapshots, you can use this single line:

Get-VM | Get-Snapshot | Remove-Snapshot -Confirm:$false

From the console, we have additional cmdlets within PowerShell that can be used to improve the process. This example gets all VMs from all connected vCenter Servers, gets any snapshots for those VMs that start with the phrase "snapshot," and displays a GUI dialog box listing the snapshots.

Get-VM | Get-Snapshot -name snapshot*| Out-GridView -PassThru | Remove-Snapshot -Confirm:$false
Example of query results presented in a grid view

Example of query results presented in a grid view

Select the snapshots you want to remove and click OK.

Subscribe to 4sysops newsletter!

You can then select the snapshots that need to be removed (for multiple selection, use either shift or ctrl), then click OK. The selected list of snapshots will then be removed. That's much easier than going through each individual virtual machine and removing the snapshots one at a time.


Leave a reply

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


© 4sysops 2006 - 2023


Please ask IT administration questions in the forums. Any other messages are welcome.


Log in with your credentials


Forgot your details?

Create Account