The Multifunctioning DBA

Sep 24 2009   4:13PM GMT

Powershell Remove files

Colin Smith Colin Smith Profile: Colin Smith

I have a server that attempts to backup up the SQL Server databases and it always fails because not enough space is available. This usually is not a big deal as I would run a SQL Clean up task before the backups run to remove the old files. In this case though I am using a third party utility to run the backups and it does not have that functionality. So, since I am no sqljockey yet, I have chosen Powershell as my tool. Here is what I have done.

$days = [datetime]::now.adddays(-7) E: cd \ cd Directory ls | where {$ -like "*.bak"} | rm -force cd tranlogs ls | where {$ -like "*trn"} | rm -force cd ../../Directory ls | where {$ -like "*.bak"} | rm -force cd tranlogs ls | where {$ -like "*bak"} | rm -force ls | where {$ -like "*txt"} | where{$_.creationtime -lt $days} | rm -force c:

You can see that I simple cd to the directory that I have my backupfiles in and do an rm or Remove-object. I am also keeping 7 days worth of the txt log files that the backups create. Just in case I need them. Hope that helps. Enjoy

 Comment on this Post

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: