Can you delete a windows task with another task trough powershell?


When using following powershell script which creates a scheduled task to delete another scheduled task, I am confronted with an error (access denied: 0x80070005). When I manually define the task and execute it, it works fine.

# Prep task 2 for taskscheduler > delete task 1 after reboot if not deleted before
$Action2    = New-ScheduledTaskAction -Execute "C:\Windows\system32\cmd.exe" -Argument "/c schtasks /delete /tn 'Computer Forced Reboot'/f"
$Trigger2   = New-ScheduledTaskTrigger -AtLogon
$Settings2  = New-ScheduledTaskSettingsSet -DontStopIfGoingOnBatteries -Compatibility Win8 -RestartCount:10 -RestartInterval (New-TimeSpan -Minutes 1)
$Principal2 = New-ScheduledTaskPrincipal -UserID "domain\$env:UserName" -LogonType Interactive
# Register task 2 to Taskscheduler
Register-ScheduledTask -TaskName "test test" -Action $Action2 -Trigger $Trigger2 -Settings $Settings2 -Principal $Principal2here

What am I doing wrong? Is this not possible (specific rights) trough powershell? Is there a workaround?

asked on Stack Overflow Feb 8, 2021 by user3693898

0 Answers

Nobody has answered this question yet.

User contributions licensed under CC BY-SA 3.0