Home » PowerShell » PowerShell Delete Scheduled Task using Unregister-ScheduledTask

PowerShell Delete Scheduled Task using Unregister-ScheduledTask

To delete a scheduled task in Windows, use the Unregister-ScheduledTask cmdlet in PowerShell. This command unregisters the scheduled task. It prompts for confirmation to perform the delete operation on the task.

Unregister-ScheduledTask -TaskName "UserTask"

Unregister-ScheduledTask removes a scheduled task from the Windows Task scheduler service on a local computer or the remote computer using the CimSession object.

In this article, we will discuss how to delete scheduled tasks using the PowerShell command.

PowerShell Remote Scheduled Task using Unregister-ScheduledTask

Use the PowerShell cmdlet Unregister-ScheduledTask to unregister a task from the Task Scheduler service.

Unregister-ScheduledTask -TaskName "Remove Log File Task"

The Unregister-ScheduledTask command in PowerShell uses the TaskName parameter to delete a scheduled task name.

It will prompt for confirmation to delete the scheduled task, and confirm Y to perform the action.

The output of the above PowerShell script to remove scheduled tasks is:

PowerShell Remove Scheduled Task
PowerShell Remove Scheduled Task
PS C:\> Unregister-ScheduledTask -TaskName "Remove Log File Task"
                                                       
Confirm

Are you sure you want to perform this action?                                                                           Performing operation 'Delete' on Target '\Remove Log File Task'.                                                        [Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): Y  
                             
PS C:\>                                                                                                                            

To suppress the confirmation prompt, use the -Confirm:$false

Unregister-ScheduledTask -TaskName "Remove Log File Task" -Confirm:$false

Delete Scheduled Task in PowerShell

If you run the Unregister-ScheduledTask command to delete a scheduled task that doesn’t exist, it will throw an exception. Check if the task exists using the Get-ScheduledTask cmdlet in PowerShell.

# Use the Get-ScheduledTask to get task details
$task = Get-ScheduledTask | Where-Object {$_.TaskName -eq "Remove Log File Task"} | Select-Object -First 1    
if($task -ne $null){
   Unregister-ScheduledTask $task.TaskName -Confirm:$false
} 
else {
   Write-Host "Scheduled task doesn't exist."
} 

In the above PowerShell script, the Get-ScheduledTask cmdlet retrieves the scheduled task details. This command uses the Where-Object condition to check if the task name is equal to the specified task name and selects the first object and store it in the $task variable.

If statement checks for the $task variable value if it contains the scheduled task or is it empty. If it contains the task, it returns the true value and executes the Unregister-ScheduledTask command to delete a task, else it prints the message “Scheduled task doesn't exist.

The output of the above PowerShell script is:

PS C:\> $task = Get-ScheduledTask | Where-Object {$_.TaskName -eq "Remove Log File Task"} | Select-Object -First 1

PS C:\> $task                                                                                                             

PS C:\> if($task -ne $null){Unregister-ScheduledTask -TaskName $task.TaskName -Confirm:$false} else {Write-Host "Scheduled task doesn't exist."}                                                                                                    

Scheduled task doesn't exist.

PS C:\>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 

Conclusion

I hope the above article on how to delete a scheduled task in PowerShell using the Unregister-ScheduledTask command is helpful to you.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.