In SQL Job, create the following step
Type: PowerShell
Run as: SQL Server Agent Service Account
Command:
$Path = "D:\MSSQL\SQLBackups\"
$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