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 Create and Manage Scheduled Tasks Using PowerShell

The "New-CMSchedule" cmdlet is not a native Windows PowerShell command. However, Windows provides robust tools for creating and managing scheduled tasks through PowerShell and the Task Scheduler. Scheduled tasks are essential for automating repetitive tasks, ensuring regular maintenance, and executing scripts at specific times without manual intervention. This article will guide you on how to create and manage scheduled tasks using PowerShell, providing practical examples to help you understand and implement these tasks effectively.

Examples:

  1. Creating a Basic Scheduled Task:

    To create a scheduled task that runs a PowerShell script daily at a specific time, use the New-ScheduledTaskTrigger, New-ScheduledTaskAction, and Register-ScheduledTask cmdlets.

    # Define the trigger - Daily at 8 AM
    $trigger = New-ScheduledTaskTrigger -Daily -At 8am
    
    # Define the action - Run a PowerShell script
    $action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument '-File "C:\Scripts\MyScript.ps1"'
    
    # Register the scheduled task
    Register-ScheduledTask -TaskName "DailyScriptTask" -Trigger $trigger -Action $action -Description "Runs MyScript.ps1 daily at 8 AM"
  2. Creating a Scheduled Task with Specific User Credentials:

    Sometimes, you may need to run a task with specific user credentials. Use the -User and -Password parameters with Register-ScheduledTask.

    # Define the trigger - Weekly on Mondays at 9 AM
    $trigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Monday -At 9am
    
    # Define the action - Run a batch file
    $action = New-ScheduledTaskAction -Execute 'cmd.exe' -Argument '/c "C:\Scripts\MyBatchFile.bat"'
    
    # Register the scheduled task with user credentials
    Register-ScheduledTask -TaskName "WeeklyBatchTask" -Trigger $trigger -Action $action -User "DOMAIN\UserName" -Password "UserPassword" -Description "Runs MyBatchFile.bat weekly on Mondays at 9 AM"
  3. Listing All Scheduled Tasks:

    To list all scheduled tasks on your system, use the Get-ScheduledTask cmdlet.

    # List all scheduled tasks
    Get-ScheduledTask
  4. Removing a Scheduled Task:

    If you need to remove a scheduled task, use the Unregister-ScheduledTask cmdlet.

    # Remove a scheduled task by name
    Unregister-ScheduledTask -TaskName "DailyScriptTask" -Confirm:$false
  5. Modifying an Existing Scheduled Task:

    To modify an existing scheduled task, you can use the Set-ScheduledTask cmdlet. For example, to change the trigger time:

    # Get the existing task
    $task = Get-ScheduledTask -TaskName "DailyScriptTask"
    
    # Modify the trigger to run at 7 AM instead of 8 AM
    $task.Triggers[0].StartBoundary = "2023-10-01T07:00:00"
    
    # Update the task
    Set-ScheduledTask -TaskName "DailyScriptTask" -InputObject $task

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.