Created
May 13, 2011 15:23
-
-
Save ahill00/970730 to your computer and use it in GitHub Desktop.
PowerShell Script for SQL Server Backup Removal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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