12

Windows 7 box has a list of scheduled tasks - > Default Windows tasks + Certain custom tasks that all start with a certain string. Previous code deleted all tasks with the command:

schtasks /Delete /TN * /F

Instead we want to retain the default Windows tasks, and selectively delete the tasks that start with a certain string say abctask1, abctask2. How to proceed further? Thanks!

3
  • Have you tried using something like schtasks /Delete /TN abctask* /F? Sep 20, 2012 at 13:56
  • 1
    Doesnt work. I think the * wildcard doesn't work as expected.
    – user1620230
    Sep 20, 2012 at 14:56
  • If the wildcard doesn't work properly in that manner, your best bet might be to obtain the list of tasks with the /query option, loop through them finding the ones that match your desired pattern, and kill them off using their full names. Something like a Perl script might be best for this kind of task. Sep 20, 2012 at 17:21

5 Answers 5

6

Assuming your tasks are one word (e.g., abctask1, abctask2 - not "abc task 1"), this should work:

for /f %%x in ('schtasks /query ^| findstr abctask') do schtasks /Delete /TN %%x /F
6
  • 1
    I would imagine that find, being less than 1/4 the size of findstr would react faster. Also, both support the use of quotation marks to surround the search term (find requires them), doing that will allow finding tasks like the "abc task 1" you mentioned.
    – James K
    Sep 21, 2012 at 6:32
  • Thanks Mark, well actually it did not accept %%x, I changed it to %x and it worked! :) Cheers! :)
    – user1620230
    Sep 21, 2012 at 13:47
  • @user1620230 I assumed you were running this in a batch file. The double-percents in %%x are required in a batch file, whereas %x is required at the command line. @JamesK, I think you'd be very hard pressed to find any real-world speed difference in find vs. findstr, particularly in something this small. :-)
    – Mark
    Sep 21, 2012 at 17:28
  • Aha, got you :) Well yes ultimately it might be used in a batch script. Thanks for the info :)
    – user1620230
    Sep 21, 2012 at 19:49
  • schtasks /query truncates task names to 50 characters. The /xml option avoids truncation. for /f "tokens=2" %%x in ('schtasks /query /xml ^| findstr taskname') do
    – David
    Sep 14, 2018 at 16:46
3

If you want wildcard (*) to be used in your selection of tasks to delete, try using this simple batch command:

(sample only)

echo off
del %SystemDrive%\Windows\Tasks\Google*
del %SystemDrive%\Windows\Tasks\Facebook*
2

I ended up here while looking for a way to automate disabling some stock Windows scheduled tasks. I could not get any of the current examples to properly handle task names with backslashes and spaces. I found that using csv format and the find command worked best for me:

for /f "tokens=1 delims=," %%x in ('schtasks /query /fo csv ^| find "\Microsoft\Windows\Application Experience\"') do schtasks /Delete /TN %%x /F
2

You guys have helped here quite a bit. While the above works for the shorter names and 'user3490' posted quite a nice addition to the string, it still did not seem to work with names with spaces. I found using wildcards with SCHTASKS work with '.' rather than '*' and found an interesting modification to the string allowing much needed opportunity! I'll paste below examples of what I used in a batch file:

for /f "tokens=2 delims=\" %%x in ('SCHTASKS /QUERY /FO:LIST ^| FINDSTR "AVGPCTuneUp_Task."') do SCHTASKS /DELETE /F /TN "\%%x"

for /f "tokens=2 delims=\" %%x in ('SCHTASKS /QUERY /FO:LIST ^| FINDSTR "AMD.Updater."') do SCHTASKS /DELETE /F /TN "\%%x"

for /f "tokens=2 delims=\" %%x in ('SCHTASKS /QUERY /FO:LIST ^| FINDSTR "Lenovo.Smile."') do SCHTASKS /DELETE /F /TN "\%%x"

Thank you all here for the excellent pointers! I am aware this topic is over a year old, but I hope this helps someone else who may have not gotten this far.

1

Mark's answer works fine as long as you have fairly short scheduled task names, but the default output of schtasks /query (table format) truncates the task names to 32 characters, which causes errors when trying to delete them.

If you need to deal with tasks that have longer names, I would recommend using list output format instead, which does not truncate:

for /f "tokens=2 delims=\" %%x in ('schtasks /query /fo:list ^| findstr abctask') do schtasks /Delete /TN %%x /F

If the tasks you want to delete have a common prefix, you can further refine the match from findstr:

for /f "tokens=2 delims=\" %%x in ('schtasks /query /fo:list ^| findstr ^^abctask') do schtasks /Delete /TN %%x /F

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .