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

23,720

Solution 1

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.

Solution 2

Create a scheduled task, and run it as a given user.

enter image description here

Share:
23,720

Related videos on Youtube

bwDraco
Author by

bwDraco

I'm a computer and tech enthusiast with a variety of interests ranging from video games to fountain pens. I stand behind everything I post here. If you find any problems with my posts, feel free to leave comments, edit them, or let me know in chat if you have any questions or concerns. If you're curious about my answering style, it's inspired by the answers posted by Thaddeus Howze ♦ at Science Fiction & Fantasy. This chat transcript details why I made this change. I'm also one of the resident spam fighters on Super User and can catch spam others miss. If you suspect that one or more posts are spam, bring it up in the Ask a Super User Moderator chat room, and be sure to ping me—I'm around most of the time. Don't forget to flag blatant spam—it takes just six spam flags from the community to delete it. It sounds like flagging posts as spam isn't enough; they need to be flagged specifically to your attention (and what are the odds that you haven't already found it). —fixer1234 (source) If you're wondering about my name, it used to be DragonLord, but I've had to change it due to a bizarre trademark dispute. Feel free to call me Draco or by my real first name Brian. As an aside, I'm a bit of a photo enthusiast. I shoot with Pentax eqiuipment, and you can find me on Pentax Forums.

Updated on September 18, 2022

Comments

  • bwDraco
    bwDraco over 1 year

    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?