Selectively deleting system scheduled tasks

10

3

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!

Kimi

Posted 2012-09-20T12:57:45.277

Reputation: 201

Have you tried using something like schtasks /Delete /TN abctask* /F? – Jonah Bishop – 2012-09-20T13:56:57.490

1Doesnt work. I think the * wildcard doesn't work as expected. – None – 2012-09-20T14:56:02.150

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. – Jonah Bishop – 2012-09-20T17:21:59.267

Answers

5

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

Mark

Posted 2012-09-20T12:57:45.277

Reputation: 166

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 – 2018-09-14T16:46:09.137

1I 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 – 2012-09-21T06:32:06.273

Thanks Mark, well actually it did not accept %%x, I changed it to %x and it worked! :) Cheers! :) – None – 2012-09-21T13:47:02.227

@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. :-) – None – 2012-09-21T17:28:40.513

Aha, got you :) Well yes ultimately it might be used in a batch script. Thanks for the info :) – None – 2012-09-21T19:49:39.883

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*

Lyn1214

Posted 2012-09-20T12:57:45.277

Reputation: 31

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

bskip

Posted 2012-09-20T12:57:45.277

Reputation: 71

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.

Andrew A Kratz

Posted 2012-09-20T12:57:45.277

Reputation: 21

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

user3490

Posted 2012-09-20T12:57:45.277

Reputation: 484