One of the most important tasks for a database administrator is to backup all the databases for security reasons. As a Database administrator we need to make sure that in case of a disaster we will be able to recover our data from any point of time, that’s one of the reasons why every Database server should have a Backup maintenance plan. A common practice when implementing this kind of plans is to perform a backup into the server and then move the backup files to a storage server or copy the files in a tape. Once the backups have been moved or copied to the tape we need to purge the backup files that we still have in our server. It doesn’t make sense to keep all the backup files into the server when we already have moved those files to a safer place. So depending on the maintenance plan and business requirements we will want to maintain more or less historical backup files in our servers. In this post we will see how to do it easily with PowerShell.
#Function to delete files older than a date 
Function Delete-Files([string] $Path, [String] $Filefilter, [int] $DaysThreshold) 
    get-ChildItem -Path $Path -Filter $Filefilter | where {$_.creationTime -lt (Get-Date).AddDays($DaysThreshold*(-1))} | remove-Item 
#Execution sample 
Delete-Files -Path "C:\tmp" -Filefilter "*.bak" -DaysThreshold 1