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

Last modified: November 26, 2019

Author

Comments

Write a Reply or Comment