Categories
Uncategorized

Windows PowerShell Remove previous Hyper-V backup folders older than….

This is based on code from Mike Galvin. When you copy the dirs to a network drive or so, this allows you to delete older backups.

Create a file called “Remove-old-Backups.ps1” and enter the following code:

##Remove previous backup folders older than the configured number of days.
$Backup = "\\NETWORKDRIVE\FOLDER\"
$History = 4
## Location of the VM names file
$VmList = "c:\scripts\vms.txt"
##If a VM list file is configured, get the content of the file.
If ($VmList)
{
$Vms = Get-Content $VmList
echo $Vms
}
ForEach ($Vm in $Vms)
{
Get-ChildItem -Path $Backup -Filter "$Vm
-*-*-*-*-*-*" -Directory | Where-Object CreationTime –lt (Get-Date).AddDays(-$History) | Remove-Item -Recurse -Force
echo $Backup
}

Source: https://gal.vin/2017/09/18/vm-backup-for-hyper-v/