dbatools is a free PowerShell module with over 300 SQL Server administration, best practice and migration commands included.

Please note that documentation and command names may be out of date while we work furiously towards 1.0


Find-DbaBackup

Provides all of the same functionality for finding SQL backups to remove from disk as a standard maintenance plan would.

As an addition you have the ability to check the Archive bit on files before deletion. This will allow you to ensure backups have been archived to your archive location before removal.

Screenshots

dbatools-Find-DbaBackup

Examples

‘*.trn’ files in ‘C:\MSSQL\SQL Backup\’ and all subdirectories that are more than 48 hours old will be included.

Find-DbaBackup -Path ‘C:\MSSQL\SQL Backup\’ -BackupFileExtension trn -RetentionPeriod 48h

‘*.bak’ files in ‘C:\MSSQL\Backup\’ and all subdirectories that are more than 7 days old will be included, but only if the files have been backed up to another location as verified by checking the Archive bit.

Find-DbaBackup -Path ‘C:\MSSQL\Backup\’ -BackupFileExtension bak -RetentionPeriod 7d -CheckArchiveBit

Author

This command was created by Chris Sommer. You can find Chris on Twitter and his blog.

Get-Help

From PowerShell, execute Get-Help Find-DbaBackup -Detailed for more information on this function.

Source Code

Want to see the source code? View Find-DbaBackup.ps1 on GitHub
 

Related commands