Skip to content

Instantly share code, notes, and snippets.

@ahill00
Created May 13, 2011 15:23
Show Gist options
  • Save ahill00/970730 to your computer and use it in GitHub Desktop.
Save ahill00/970730 to your computer and use it in GitHub Desktop.
PowerShell Script for SQL Server Backup Removal
param(
[string]$path,
[int]$days
)
# Recursively looks through a specified folder and deletes files of .bak and .trn extensions older than 30 days.
# USAGE: .\db-backup-removal.ps1 $path $days
# EXAMPLE: .\db-backup-removal.ps1 "C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\" 30 -- prints all files in folder recursively to be removed
# Remove -whatif and it will actually delete the files
# http://stackoverflow.com/questions/314679/why-arent-my-sql-server-2005-backups-being-deleted
$days = $days * -1
# we are going back in time, not forward
dir $path -recurse -include *.bak, *.trn | ? {$_.CreationTime -lt (get-date).AddDays($days)} | del -whatif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment