Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Schedule Tasks in Windows Using Task Scheduler and Command Line

Agendamento online, or online scheduling, refers to the process of setting up tasks to run automatically at specified times or intervals. In the Windows environment, this is typically achieved using the Task Scheduler, a built-in utility that allows users to automate tasks on a computer. This article will guide you through the process of scheduling tasks using both the graphical interface of Task Scheduler and the command line via Command Prompt and PowerShell.

Using Task Scheduler GUI

  1. Open Task Scheduler:

    • Press Win + R to open the Run dialog.
    • Type taskschd.msc and press Enter.
  2. Create a Basic Task:

    • In the Task Scheduler window, click on "Create Basic Task" in the Actions pane.
    • Follow the wizard to set the task name, trigger (when the task should start), action (what the task should do), and any additional settings.
  3. Define Task Actions:

    • You can choose to start a program, send an email, or display a message.
    • For example, to run a script, select "Start a program" and browse to the script file.
  4. Finish and Save:

    • Review the summary and click "Finish" to save the task.

Using Command Line

  1. Using Command Prompt:

    To create a scheduled task using the Command Prompt, use the schtasks command. Here is an example:

    schtasks /create /tn "MyTask" /tr "C:\Path\To\YourScript.bat" /sc daily /st 09:00
    • /tn: Specifies the task name.
    • /tr: Specifies the task action, such as running a script.
    • /sc: Specifies the schedule frequency (e.g., daily, weekly).
    • /st: Specifies the start time.
  2. Using PowerShell:

    PowerShell provides more flexibility and scripting capabilities. Here is an example of creating a scheduled task using PowerShell:

    $action = New-ScheduledTaskAction -Execute "C:\Path\To\YourScript.ps1"
    $trigger = New-ScheduledTaskTrigger -Daily -At 9AM
    $principal = New-ScheduledTaskPrincipal -UserId "NT AUTHORITY\SYSTEM" -LogonType ServiceAccount
    Register-ScheduledTask -Action $action -Trigger $trigger -Principal $principal -TaskName "MyTask"
    • New-ScheduledTaskAction: Defines the action to execute.
    • New-ScheduledTaskTrigger: Defines when the task should run.
    • New-ScheduledTaskPrincipal: Defines the user context for the task.
    • Register-ScheduledTask: Registers the task with the specified settings.

Examples:

  • To schedule a batch script to run every day at 10 AM using Command Prompt:

    schtasks /create /tn "DailyBackup" /tr "C:\Scripts\backup.bat" /sc daily /st 10:00
  • To schedule a PowerShell script to run every Monday at 8 AM:

    $action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-File C:\Scripts\weekly_report.ps1"
    $trigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Monday -At 8AM
    Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "WeeklyReport"

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.