Microsoft windows provides an ability for the user to schedule the launch of a program or script at pre-defined time or when one or more conditions are met using a Task scheduler component.
User gets an ability to define the conditions like when the hard disk space is 80% consumed, run a cleanup script or send an email to list of users. In this blog, I am sharing the steps to launch a logs cleanup powershell script as a scheduled task.
- Open the Windows Task Scheduler in the Start menu, under Start > Administrative Tools
- In Task Scheduler, select the Create Task option under the Actions heading on the right-hand side.
- Enter a name for the task, and give it a description (the description is optional).
- In the General tab, go to the Security options and specify the user account that the task should be run under. Change settings so the task will run even if the user is not logged in.
- Next, select the Triggers tab, and click New to add a new trigger for the scheduled task. This new task should use the “On a schedule” option. The start date can be set to the desired time, and the frequency and duration of the task can be set based on your specific needs. Click OK when your desired settings are entered.
- Next, go to the Actions tab and click New to set the action for this task to run. Set the Action to Start a program.
- In the Program/script box enter “PowerShell”
- In the Add arguments (optional) box enter the value “.\[Your PowerShell Script Name].” For example, if your PowerShell Script is named “TestScript.ps1” then you would enter “.\ TestScript.ps1” as the value.
- Then, Start button in the (optional) box, add the location of the folder that contains your PowerShell script. In this example, the script is in a folder called “Script” that is off the root C: drive.
Note: The location used in the Start in box will also be used for
- storing the scheduled task run times
- the job history for the copies
- any additional logging that may occur.
- Click OK when all the desired settings are made.
- Next, set any other desired settings in the Conditions and Settings tabs.
- Once all the desired actions have been made (or added), click OK. The task will be immediately set, and is ready to run.
- To check if the task is set properly, right-click on the task and hit “Run”