1. Home
  2. Knowledge Base
  3. Windows server
  4. Steps to create the Snapshot Script

Steps to create the Snapshot Script

Step 1: Log into your server through Remote Desktop.
Step 2: Run PowerShell from the Start menu or your desktop.
Step 3: Run the following script.

++
set-executionpolicy RemoteSigned
++

Step 4: When the Execution Policy Change warning is displayed enter Y.
Step 5: Open Notepad and paste the following contents into it. (Note line 1 where you can change the number of days for your rotation):

++
$Days = 7 #change this value to determine the number of days to rotate.

$VMs = Get-VM
foreach($VM in $VMs){
$Snapshots = Get-VMSnapshot $VM

foreach($Snapshot in $Snapshots){

if ($snapshot.CreationTime.AddDays($Days) -lt (get-date)){
Remove-VMSnapshot $Snapshot
}
}

Checkpoint-VM $VM
}
++

Step 6: Save the file as C:\SnapshotVMs.ps1
Step 7: Open Task Scheduler (press your Windows key and start typing Task Scheduler).
Step 8: In the Actions section click Create Basic Task and follow the wizard to set up the following parameters:
Name: CUSTOM – SNAPSHOTVMS > Next
Trigger: Daily > Next
Daily: select 11:00:00 PM Recur every: 1 days > Next.
Action: Start a program > Next.
Program/Script: type PowerShell
Add Arguments: type C:\SnapshotVMs.ps1
Step 9: Check the box for “Open the Properties dialog for this task when I click Finish” and click Finish.
Step 10: In the General tab, in the Security Options section, select the option to Run whether user is logged on or not. Click OK.
Step 11: Enter the Administrator credentials that you use to log into your server. Click OK.
Step 12: In Task Scheduler click on Task Scheduler Library.
Step 13: Locate and right click on the CUSTOM – SNAPSHOTVMS task you created.
Step 14: Run the scheduled task to verify that it executes correctly. If successful, you will see a PowerShell console open and a snapshot created for all servers in Hyper-V Manager.
Step 15: Log in the following day to verify that all VMs on the server have a snapshot on them in Hyper-V Manager.

Related Articles