SQL job to remove bak files older than 2 days for a folder.
Type: PowerShell
Run as: SQL Server Agent Service Account
Command:
$Path = "D:\MSSQL\DBBackups\"
$Daysback = “-2”
$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddDays($Daysback)
Get-ChildItem $Path -Recurse | Where-Object { $_.LastWriteTime -lt $DatetoDelete -and $_.Extension -eq ".bak" } | Remove-Item -Recurse -ErrorAction SilentlyContinue
Comments