Automating Weekly MS SQL Database Backups Using PowerShell and Task Scheduler
Step 1: Understand the Goal
In this tutorial, you will create an automated solution that:
Takes a full backup of a specific MS SQL Server database
Stores backups in a folder
Keeps only the latest 5 backups
Automatically deletes the oldest backup when a new one is created
Runs every Sunday using Windows Task Scheduler
This solution works well even on SQL Server Express, where SQL Agent is not available.
Step 2: Prerequisites
Before starting, make sure you have:
Microsoft SQL Server installed
PowerShell 5.1 or later
Permission to back up the SQL database
Write access to the backup folder
SqlServerPowerShell module installed
To install the SQL module (run PowerShell as Administrator):
Step 3: Create the Backup Folder
Decide where the backup files will be stored, for example:
Make sure this folder exists and the scheduled task account has write permissions.
Step 4: Create the PowerShell Backup Script
Open Notepad or PowerShell ISE and paste the following script.
Save the file as:
Step 5: Test the Script Manually
Before scheduling, test the script manually.
Open PowerShell as Administrator
Run:
Verify that:
A
.bakfile is createdNo errors appear
Old backups are deleted after the 5th run
Step 6: Set PowerShell Execution Policy
If scripts do not run, enable script execution:
Select Y when prompted.
Step 7: Open Task Scheduler
Press Win + R
Type
taskschd.mscPress Enter
Step 8: Create a Scheduled Task
Click Create Task
In the General tab:
Name:
Weekly SQL Database BackupSelect Run whether user is logged on or not
Check Run with highest privileges
Step 9: Configure the Trigger (Weekly)
Go to the Triggers tab
Click New
Select:
Begin the task: On a schedule
Schedule: Weekly
Day: Sunday
Time: Choose a low-usage time (e.g., 2:00 AM)
Click OK
Step 10: Configure the Action
Go to the Actions tab
Click New
Set:
Program/script:
powershell.exeAdd arguments:
Start in:
Click OK
Step 11: Configure Conditions and Settings
Conditions Tab
Uncheck Start the task only if the computer is on AC power
Settings Tab
Check Allow task to be run on demand
Check Run task as soon as possible after a scheduled start is missed
Step 12: Save and Test the Task
Click OK
Enter the account password when prompted
Right-click the task → Run
Verify a new backup file is created
Step 13: Verify Ongoing Operation
Each Sunday, confirm:
A new
.bakfile appearsOnly 5 backup files exist
Task Scheduler shows Success in History
Conclusion
By following these steps, you have successfully implemented an automated, reliable weekly backup system for your SQL Server database using PowerShell and Task Scheduler. This approach is simple, cost-effective, and ideal for environments without SQL Agent.
Last updated
Was this helpful?