Is it possible to make a batch file run on startup without putting the file in the startups folder manually when you execute it once?
Asked
Active
Viewed 423 times
0
-
1Does this answer your question? [Run Batch File On Start-up](https://stackoverflow.com/questions/21218346/run-batch-file-on-start-up) – Binu Apr 14 '20 at 13:49
-
task scheduler, startup folder, registry entries, external tools... too many options to choose from – ScriptKidd Apr 14 '20 at 14:24
-
If you only want to run it once, you could still place a **shortcut** to the batch file inside the startup directory, _(yes, that's correct, that directory is not for other file types)_, and self delete the script on completion. To do that, replace any `Exit`/`GoTo :EOF` instruction in your script with `@(GoTo) 2>Nul & Del "%~f0"`. If you don't currently have an `Exit`/`GoTo :EOF` instruction, just add `@(GoTo) 2>Nul & Del "%~f0"` as your new last line. _Please note that this idea may be subject to having the required delete permissions._ – Compo Apr 14 '20 at 17:33
1 Answers
0
I'd advise you to add a schtasks
entry in your batchfile, using the following switches and parameters:
schtasks ... /SC ONSTART ... /TR <batchfile> ...
This creates a scheduled task, referring to your batchfile (the file itself), and is scheduled at startup.
An example of this is:
schtasks /SC ONSTART /TR C:\MyDir\bin\LaunchMe.bat

Dominique
- 16,450
- 15
- 56
- 112
-
-
-
@TheOntley: the ... mean that you might fill in something else, like startdate, enddate, in case you're interested, but that's up to you. You can leave this blank too. As for the
, this is the full path of the batchfile, something like 'C:\MyDir\bin\LaunchMe.bat'. I edited my answer accordingly. – Dominique Apr 15 '20 at 16:08 -
@TheOntley: Oops, sorry, I've written `/TN` while it should be `/TR`, I've corrected my answer. – Dominique Apr 15 '20 at 16:12
-
I figured it out from looking at the command and options online, though I used schtasks create and messed around with it in powershell, is that necessary or will what you provided still work because yours doesn't specify a name and such – ontley Apr 15 '20 at 21:50
-
I do believe it will work indeed, I see no reason why PowerShell would need to be involved. – Dominique Apr 15 '20 at 22:35
-
Just used it so I don't have to run the bat file everytime and just create it instantly, gave me errors like "mandatory option /tr is missing" and such – ontley Apr 15 '20 at 23:54
-
@TheOntley: did you see that I first mistyped TN and replaced it by TR in my answer? – Dominique Apr 16 '20 at 11:39