Windows Power Shell script to Purge/Cleanup Backup & Transaction files

At times, we need to purge backup files (.bak, *.trn) which are older than some x days from the server on the regular basis, such that Disk Space and SQL Data backups will be maintained consistently within the server.

Power Shell Script:-

The below power shell script will purge the backup files (which are older than 5 days) from server, this script identifies the older files based on the last modified date time.

Based on your requirement, you can change the date range and schedule this script for Server Maintenance.

Get-ChildItem -Path "C:\Backups" -Recurse -ErrorAction SilentlyContinue -include *.bak, *.trn | 
Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-5) -and $_.PSIsContainer -eq $False} | 
Remove-Item

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s