Powershell Remove files
Posted by: 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 {$_.name -like "*.bak"} | rm -force cd tranlogs ls | where {$_.name -like "*trn"} | rm -force cd ../../Directory ls | where {$_.name -like "*.bak"} | rm -force cd tranlogs ls | where {$_.name -like "*bak"} | rm -force ls | where {$_.name -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



You must be logged-in to post a comment. Log-in/Register