posted 6/12/2012 by sqlscottgleason - Views: [5616]
I ran across a server that has backups being generated and it doesn’t have any maintenance plans created on the server itself. The backups are being created from some external source.
The disk drives are being filled up as there is nothing that is deleting these backup files that keep getting created each night.
So I had to solve two issues; 1. Delete the old backups 2. Find out what is causing backups to be created each night.(I'll post another blog and how I find what cuasing those backsup each night)
I did a search for a PowerShell script that would delete backups, but found several examples that were close or did not work. My final working solution is as follows:
$Path = "E:\SQLServer\backup"
$Daysback = "-5"
$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddDays($Daysback)
Get-ChildItem $Path -Recurse -include *.txt, *.bak| Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item