Run a command on Windows startup (not login!)

31,934

Solution 1

Define a startup script with your desired BAT file. Do this either via Local Computer Policy (for standalone machines) or via GPO (if in a domain.)

Solution 2

If you need to set the startup script programatically, you can use Scheduled Tasks:

schtasks.exe /create /tn "Task Name" /ru SYSTEM /Sc ONSTART /tr
"C:\command2run.cmd"

To run the task with elevated permissions (Vista and later), add the parameter /RL HIGHEST

Solution 3

Use Group Policy to run a Computer Startup script - this will run when the machine starts up without needing someone to login

Share:
31,934

Related videos on Youtube

Steve Townsend
Author by

Steve Townsend

Systems Administrator, Consultant, Programmer, Linux geek, Computer geek, Networking geek. I work for Net Direct, Canada's leading Linux solutions provider and largest Canadian IBM System x VAR. LinkedIn Profile Achievements Unlocked

Updated on September 18, 2022

Comments

  • Steve Townsend
    Steve Townsend over 1 year

    How do you tell Windows to run a command on each system startup?

    I have a large number of servers on which I will need to run a command on system startup that makes a non-persistent configuration change.

    I can run the command either:

    • as an executable with multiple parameters
    • as a batch file that calls above executable with appropriate parameters

    How do I instruct Windows to run my command on every boot, without any sort of login?

    related: https://stackoverflow.com/q/5899978/93180

  • Steve Townsend
    Steve Townsend about 13 years
    How do I programmatically merge my changes into the computer policy? Do I just create a new key under HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Scripts\Startup and put the appropriate entries there?
  • mfinni
    mfinni about 13 years
    You're generally not supposed to manually touch anything in GPOs, AFAIK. And scripting options for making specific changes to GPOs are fairly lacking, from what I recall. You can export, import, create/delete, and link GPOs from scripts, but I don't think you can make specific changes to specific GPOs (easily.) Anyway, that's a different question, and one probably better suited to StackOverflow.
  • Steve Townsend
    Steve Townsend about 13 years
    Yeah, writing up my question now. I'll need to add an entry to the GPO as part of an installer.
  • Steve Townsend
    Steve Townsend about 13 years
    Actually, now that I know what to look for: nsis.sourceforge.net/LGP_Startup/Shutdown_Script
  • Jaans
    Jaans over 9 years
    Much nicer (and simpler to do) solution!! You can also use the Task Scheduler GUI if you prefer.