Windows – Run Windows batch files at startup or when any user logs on

automationbatch filebootwindows

I have a server access where I want some batch files to run automatically for any user at startup.

The problem is:

  1. Other users are unable to run this file and it is required to be run as the user who created the file (Don't know why?, possibly because they don't have admin privileges)

  2. Even with runas they are prompted for a password. I dont have a problem sharing the password but don't know how the task will be automated.

I tried runas /savecred /user:Username "c:/xyz.bat" but it fails. The command runas works with /netonly but then it prompts for a password.

Is there some way I can get the batch script to run for any user?

Best Answer

If you are running Windows Server or a Professional edition of Windows, you can do this using Group Policy. The scripts will be run as the user who is logging on, whoever that may be, with their respective permissions. They will not be tied to a specific user.

Save your batch scripts in C:\Windows\System32\GroupPolicy\User\Scripts\Logon. Then open Group Policy Editor (gpedit.msc), go to User Configuration > Windows Settings > Scripts (Logon/Logoff), then select Logon on the main panel. Click on Add in the Logon Properties dialog, then Browse to select the script. Repeat this for each script you want to run at logon. For more detailed information, see this Microsoft article.

Adding logon scripts using Group Policy Editor

Note that the above process is for users logging on. If you want scripts to run at system startup with Local System permissions, go to Computer Configuration > Windows Settings > Scripts (Startup/Shutdown) and select Startup, then follow the rest of the procedure above. Scripts for system startup should be stored in C:\Windows\System32\GroupPolicy\Machine\Scripts\Startup.