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\”. Add this to the file:


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′”

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.



Contact Us

On Key

More Posts

WME Security Briefing 27 May 2024

Kinsing Hacker Group Exploits Docker Vulnerabilities Overview Recent investigations have shown that the hacker group Kinsing is actively exploiting Docker vulnerabilities to gain unauthorized access to systems. The modified hacker group targets misconfigured Docker API ports deployed with cryptocurrency mining malware.

Read More »
WME Cybersecurity Briefings No. 010
Cyber Security

WME Security Briefing 20 May 2024

Advanced Persistent Threats: North Korean Hackers Launch Golang Malware Overview A new malware strain, called Titan Stealer, is currently actively circulating in the threat landscape, targeting a variety of personal data and linked to North Korean state-sponsored cyber espionage

Read More »
WME Cybersecurity Briefings No. 009
Cyber Security

WME Security Briefing 08 May 2024

Exploitable vulnerability in Microsoft Internet Explorer, used to deploy VBA Malware Overview Cybersecurity researchers discovered a severe exploitation targeting a bug that had already been patched in the Microsoft Internet Explorer browser. Their report added that

Read More »
Be assured of everything

Get WME Services

Stay ahead of the competition with our Professional IT offerings.