8

I can create a Scheduled Task in Windows using the Register-ScheduledTask PowerShell cmdlet with a particular path (i.e., using -TaskPath "\SomePath\" and -TaskName "SomeName"). I can delete the Task using the Unregister-ScheduledTask with the same parameters. I can verify the deletion with the Windows "Task Scheduler" GUI. However, the folder ("\SomePath\") does not get deleted. While this makes perfect sense, I cannot find any way to delete this empty folder.

How do I delete an empty Task Scheduler folder using PowerShell?

UPDATE: After more research, I found another way to solve this problem within PowerShell. It involves working with a Schedule.Service object. Here is the code to delete a folder called 'My Task Folder':

$scheduleObject = New-Object -ComObject Schedule.Service
$scheduleObject.connect()
$rootFolder = $scheduleObject.GetFolder("\")
$rootFolder.DeleteFolder("My Task Folder",$null)
3
  • Your final solution throws an "access denied" error for me in last line of code. Even with "run as admin"-rights.
    – Carsten
    Commented Dec 8, 2019 at 11:46
  • Not sure why that would be, but in my case the same user that created the folder is the one deleting the folder so permissions are not an issue. To help debug, you might try deleting your empty folder using the Task Scheduler GUI so you know it is not a running-from-code problem.
    – jrsrjrsr
    Commented Dec 14, 2019 at 10:58
  • I could not even delete it via Task Scheduler GUI. Turns out that I had deleted key from the registry under TaskCache, but I still had the folder under %sys32%\Tasks. Task Scheduler dont like this combination.
    – Carsten
    Commented Dec 14, 2019 at 14:45

2 Answers 2

7

ScheduledTasks are just xml files which are stored here:

Get-ChildItem -Path 'C:\Windows\System32\Tasks'

You can just delete them from there. They are also referenced from the registry here:

HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Schedule\TaskCache

Just use the...

Remove-Item -Path 'somepath' -Force

... cmdlet as you normally would for any file or folder.

Now of course, if there is a file/folder attribute set, that is preventing the delete, just remove that.

2
  • 1
    Thank you! I never knew where Microsoft stored this info. Note that I eventually found a different method, which I think is a little more future-proof than this in the sense it would still work if Microsoft decides to change how they store the tasks. See the code in my updated question.
    – jrsrjrsr
    Commented Jul 10, 2019 at 2:07
  • 1
    No worries, glad you found a solution, but I'd doubt if MS would move this location, as it's been the way since they provided it and is still this way in Windows Server 2019 and in all the release insider preview OS (client / server) versions. Yet, always nice to have another route. You could have also just use the MS purposed built SchTaskexe. --- SchTasks /Delete /TN “My Task” --- even in your scripts.
    – postanote
    Commented Jul 10, 2019 at 3:30
1

To delete the task folder called "MEGA" in CMD (or BAT file):

powershell -c "$scheduleObject = New-Object -ComObject Schedule.Service; $scheduleObject.connect(); $rootFolder = $scheduleObject.GetFolder('\'); $rootFolder.DeleteFolder('MEGA',$null)"

Thanks to @jrsrjrsr for Powershell commands.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.