Retain ConfigMgr Backups

The backup maintenance task in ConfigrMgr is great. It gives you everything you need to restore your site. The only problem with it is that it only retains one copy of the backup, so you can’t go back days, weeks, or months to restore your environment. You can only go back to your most recent backup. Usually that’s OK, but what if that backup is corrupted, or it didn’t get a clean copy of the database? Then you could be left in a situation where you cannot restore your site.

Here is a simple PowerShell script that will keep (by default) three backups:

# create PowerShell drive for backup directory
new-psdrive -name backup -PSProvider FileSystem -root “<backup drive>”

# number of days to retain backups
$retain_count = 3

# get and format date
$date = get-date -format yyyyMMdd

rename-item backup:\<site code>Backup <site code>Backup-$date

# remove old backups
If ( (get-childitem -path backup:\).count -gt $retain_count ) {
Do { get-childitem backup:\ | select-object -first 1 | remove-item -recurse -force }
Until ( (get-childitem -path backup:\).count -eq $retain_count )
}

To change the retention, modify the line “$retain_count = 3” to the number of backups that you want to retain. For example, if I wanted to retain 7 backups, it would read:

$retain_count = 7

Also be sure to input the correct information where the text is red. First, the drive where you want the backup stored. This should match the backup location you specify in the Backup Site Maintenance task.

For the rename-item line, if my site code were 787, the line would be typed like this:

rename-item backup:\787Backup 787Backup-$date

Finally, to execute the PowerShell script after a backup, you need to modify the AfterBackup.bat file, located on your primary site server at “<ConfigMgr InstallDir>\inboxes\smsbkup.box”. Add this to the file:

@ECHO OFF

C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -executionpolicy bypass -noexit -command “. ‘<full path to PowerShell script>'”

Be sure to replace the part in red with the path to your PowerShell script. For example:

C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -executionpolicy bypass -noexit -command “. ‘D:\scripts\retain-sccm-backups.ps1′”

Disclaimer
All content provided on this blog is for information purposes only. Windows Management Experts, Inc makes no representation as to accuracy or completeness of any information on this site. Windows Management Experts, Inc will not be liable for any errors or omission in this information nor for the availability of this information. It is highly recommended that you consult one of our technical consultants, should you need any further assistance.

Share:

Facebook
Twitter
LinkedIn

Contact Us

Name
=
On Key

More Posts

Microsoft EndPoint Management

Sync ConfigMgr Collections to Intune

ConfigMgr collection sync is a feature that has been in ConfigMgr for a few versions. At a high level, it syncs the membership of a collection to a group in Entra ID that can then be

Read More »
WME Microsoft 365 Updates No. 025
Azure

WME Microsoft 365/Azure Updates 22 August 2025

1. Exchange Online: Temporary Mailbox Access Disruption via MAPI Overview Some Exchange Online users experienced mailbox access issues via the Messaging API (MAPI) on August 20, 2025. The disruption occurred after Microsoft reverted a recent service

Read More »