At one of my clients, it is desired to create a snapshot of a virtual machine each day and to maintain only a certain number of them. This script was developed in order to manage those snapshots using a scheduled task on the Hyper-V host.
To adjust the number of old snapshots retained, adjust the "-7" on the second line.
The script will cycle through all of the VMs and remove any snapshot older that $DOld.
It will then create a new snapshot using the name YYYY-M-D. The script checks to see if the VM encountered is part of a replication pair and if it is primary. This way, we only attempt to create snapshots against the primary VM in a replica pair and any non-replicating VMs.