Creating An PowerShell Script to Delete the Backup files which are older than 7 days and Adding them to Task Scheduler

Creating A PowerShell Script to Delete the Backup files which are older than 7 days and send the mail. Configure them in the Task Scheduler so that it will repeat every week.

In this Blog we will go step by step to create an PowerShell script to delete the Backup file or any types of files which is older than 7 days on the folder.

Below is the PowerShell script which should be saved as an extension .PS1.

Target Folder is a Path from where you want to delete the files.

Extension is the file extension which you want to delete (In example we have took DB backups to delete)

As per the required path save the file with .PS1 extension.

Now create an Batch file to execute the PowerShell Scripts.

Save the Batch file with .bat extension

Now both the files are ready next step is to schedule the task.

Go to the Task scheduler

Right Click on the “Task Scheduler Library” and select “Create New Task..”

Give the Name of the task > Provide the Description

Select the User to get the Task execution User

Select the “Run whether user is logged on or not” so that if the user is not logged in also it will execute.

Click on the check box “Run with Highest privileges” So that it can run with high priority.

Next go to Triggers tab

Click on “New” Button

As this Job I have scheduled to run weekly once I have selected to run weekly on Sunday once, depending on your scenarios you can change the triggers.

Enable the check box “Stop task if it runs longer than:” and provide approx. time so that the task will terminate after some time.

Got to Action Tab

Click on “New” Button

Select the Action “Start a program”

Give the path of batch file which we have created to execute the PowerShell script.

And Click “OK”

Next the Conditions Tab

Uncheck the “Start the task only if the computer is on AC power” So that it will execute as per its execution time.

Next go to Settings Tab

Once all the tab is completed, it will ask for the USER information with that credentials it should be running. Make sure the user is having permission to run the “Batch Jobs”.

The New Task will be created under the Task Scheduler Library

You can also refer other blogs on PowerShell at link

You can also refer other blogs on Microsoft at link

And also if you required any technology you want to learn, let us know below we will publish them in our site http://tossolution.com/

Like our page in Facebook and follow us for New technical information.

References are taken from Microsoft

PowerShell Script Delete Backup

2 Replies to “Creating An PowerShell Script to Delete the Backup files which are older than 7 days and Adding them to Task Scheduler”

  1. Why bother with the batch file when you can just create the task with the powershell script? Seems like extra work

Leave a Reply

Your email address will not be published. Required fields are marked *