Skip to the main content.

ScriptRunner Blog

Automate snapshots and templates with PowerCLI – Part 2

Table of contents 



Post Featured Image

In part 2 of this series, you'll learn more about the uses of templates and how to handle snapshots, such as how to create, view, and delete them. 


VMware recognized the potential of PowerShell quite early, which is why the PowerShell module (PowerCLI) of the technology company is very mature. With more than 800 cmdlets, the module enables an almost complete automation of the VMware products ESX, vCenter or even vSphere. This series of articles gives an insight into various cmdlets, which in my view play an important role in the administration of VMware environments and can facilitate the daily admin routine immensely.

The series is composed of the following articles:

  • Introduction and VM inventory management (part 1)
  • Creating snapshots and VMware templates (part 2)
  • General VMware navigation and advanced information (part 3)

In addition to classic administration and filtering tasks, PowerCLI also offers options for automating various processes, such as the creation of snapshots and the creation of templates. Both functions play a central role in managing and securing the state of virtual machines (VMs), which can save you time and effort as an administrator. In this blog article, we'll take a closer look at the practical use cases and benefits of this combination.



Although snapshots should not be considered full backups, they are still a useful tool for preserving the current state of a virtual machine. Unlike backups, which create a complete copy of the VM data, snapshots capture only the changes to the VM since the snapshot was created. This allows them to be restored quickly if needed.

With PowerCLI, you can easily create, manage, and consolidate snapshots. Here are some basic commands to help you do that:

Create snapshot of a VM:

New-Snapshot -VM <VM-Name> -Name <Snapshot-Name> -Description <Description>

View all snapshots of a VM:

Get-Snapshot -VM <VM-Name>

Deleting/removing a snapshot:

Remove-Snapshot -Snapshot <Snapshot-Name> 

Create snapshot of all VMs with tag "Test" and assign current date as snapshot name:

$VMs = Get-VM | Get-TagAssignment | Where-Object {$_.Tag -like 'Test'}
$Date = (Get-Date).ToString("dd.MM.yyyy")
Foreach ($vm in $VMs){
$vm | New-Snapshot -Memory -Quiesce -Name $Date



Automated snapshots prior to updates

One practical use case of snapshots is to automatically create snapshots before updates. Imagine that all test VMs receive their updates on a given day. With PowerCLI, you can create snapshots before the updates and consolidate them the next day, if no problems are detected. Everything can be automated and scheduled with tools like ScriptRunner.


# Variables
$SnapshotName = "PreUpdateSnapshot" 
$SnapshotDescription = "Snapshot before Updates" 
$VMTag = "Test-VM"

# get test VMs with the specified tag
$VMs = Get-VM -Tag $VMTag
# create snapshots for test VMs and start Windows updates foreach ($VM in $VMs) {
Write-Host "Create Snapshot for $($VM.Name)..."
$snapshotParams = @{
VM = $VM
Name = $SnapshotName
Description = $SnapshotDescription
New-Snapshot @snapshotParams
# Run Windows updates on the VM
$UpdateScript = {
Install-WindowsUpdate -AcceptAll -AutoReboot
Write-Host "Run Windows-Updates on $($VM.Name) ..."
$invokeScriptParams = @{
VM = $VM
ScriptText = $UpdateScript
GuestCredential = (Get-Credential)
Invoke-VMScript @invokeScriptParams


# Variables
$SnapshotName = "PreUpdateSnapshot"
$TestVMTag = "Test VM"
#  get test VMs with the specified tag
$TestVMs = Get-VM -Tag $TestVMTag
# consolidate snapshots if no problems were found
foreach ($VM in $TestVMs) {
$Snapshot = Get-Snapshot -VM $VM | Where-Object { $_.Name -eq $SnapshotName }
if ($Snapshot) {
Write-Host "Consolidate snapshot for $($VM.Name)..."
Remove-Snapshot -Snapshot $Snapshot -RemoveChildren -Confirm:$false



Templates play a crucial role in the management of virtual machines (VMs). They allow you as an administrator to quickly and efficiently create new VMs based on an existing VM.


Areas of application

Possible application areas for creating templates using PowerCLI include hands-on testing of infrastructure changes, creating a lab environment, or as a standard way to provision new virtual machines.

If you want to test a change to a productive system, you should not do this directly on the productive VM. Instead, you can create a template, create a new VM from that template with a different IP or hostname (to avoid conflicts), and run your tests on it. This minimizes the risk of downtime and unwanted effects on the productive system.

Templates are also great for setting up test or lab environments. For example, if you want to create ten training systems based on a template, simply run the following command several times:

$newVMParams = @{ 
Name = "<VM Name>"
Template = "<Template Name>"
Datastore = "<Datastore Name>"
Location = "<Folder Path>"
New-VM @newVMParams

PowerCLI allows you to easily and quickly create a template based on an existing VM. This is especially useful when you want to use a VM for testing or as a basis for new VMs. To create a template, use the following command:


$newTemplateParams = @{ 
VM = "<VM name>" Name = "<Template name>" Datastore = "<Datastore name>" Location = "<Folder path>"
New-Template @newTemplateParams

Templates can also serve as the basis for deploying new servers. Instead of using the GUI to create a new VM, you can use PowerCLI and the appropriate template to provision the VM more quickly and efficiently. This is also where using tools like ScriptRunner comes in handy to further automate and streamline this process.



By using PowerCLI and templates, you can automate time-consuming tasks and reduce the risk of errors. This allows you to work more efficiently as an administrator and focus on more important tasks. Overall, the combination of PowerShell, PowerCLI and templates is a valuable tool for managing and optimizing virtual machines. 

PowerCLI also lets you create, manage, and consolidate snapshots quickly and easily.




VMUG: Webcast

In May, ScriptRunner participated at a VMware User Group webcast:



Managing vSphere environments can be challenging, but with the right automation solution it doesn't have to be. PowerShell and the PowerCLI module are great, but typically only used by scripting experts.

What if you could use PowerShell to not only standardize and automate your recurring management tasks, but also delegate them to help desk teams and even create self-services for end users based on your scripts? This would allow you to empower these users to manage snapshots, reconfigure VMs, stop/start their VMs, or generate infrastracture reports.

Of course, not everyone is comfortable with the command-line, and you certainly don't want to give these users privileged access to your VMware environment.

So what does it take to automate and delegate your recurring tasks efficiently and securely in your organization with PowerShell?



Click here for the webcast!




Related links

About the author: