SCHTASKS - Suppress warning, answer N (NOT /F)

3

1

If I have a batch file creating a scheduled task, and it gets run multiple times, I'd like to have it just ignore subsequent attempts to create the task. I'm aware of the /F parameter that forces the task to be created, thus overwriting an existing task, but I'm wondering if there's an equivalent option to suppress the warning but NOT overwrite the task.

For example, if you run this twice:

SCHTASKS /CREATE /SC ONLOGON /TN "Test" /TR "notepad.exe"

The second time you get prompted:

WARNING: The task name "Test" already exists. Do you want to replace it (Y/N)?

Is there a way to suppress that prompt or answer N? I tried this:

ECHO N|SCHTASKS /CREATE /SC ONLOGON /TN "Test" /TR "notepad.exe"

But I get an error (which doesn't make sense to me):

ERROR: Invalid input. Valid values are (Y|N).

Is this a valid solution, can this error be safely ignored? Or should I do something else?

therks

Posted 2019-12-10T05:52:52.723

Reputation: 174

Same error comes up. Perhaps I just needn't worry about it. – therks – 2019-12-10T07:57:23.193

That would overwrite an existing task. If you fully read my post (or at least the title) you'd see I specifically don't want to use the /F parameter. – therks – 2019-12-10T09:32:47.507

Answers

1

You can try this:

<nul set /p="N" | SCHTASKS /CREATE /SC ONLOGON /TN "Test" /TR "notepad.exe"

This pipes N to the command without echoing a newline.

Wasif Hasan

Posted 2019-12-10T05:52:52.723

Reputation: 827

There we go. I knew there had to be a way to do it. So I guess echo was outputting a line break, and that's what was causing the error to appear? – therks – 2019-12-10T09:35:43.443

Yes, you are right! – Wasif Hasan – 2019-12-10T15:48:55.690