How to Fix: Run Batch Script as Administrator (Without Password)

Dennis Faas's picture

Infopackets Reader Barry S. writes:

" Dear Dennis,

I read with interest your article on how to shut down Mozilla Thunderbird gracefully using a series of 'taskkill' commands. The issue I have is that Thunderbird eats an enormous amount of memory if I leave it running over a period of 24 hours or more. When I try and send an email, the words in the email window (as I'm typing them) come out in spurts. This drives me crazy as I am a fairly quick typist. I tried setting up a batch script to run (using Task Scheduler) to gracefully close Thunderbird and restart it every 24 hours, but for some reason it isn't working properly. When Thunderbird restarts from the script, it runs in the background but doesn't load the main Thunderbird window. Strangely enough this works fine if I run the script using an administrative command prompt window. I believe the problem has to do with administrative access, even though I have set the Task Scheduler to run the program as Administrator. Can you help me run a batch script as administrator? "

My response:

I asked Barry if he would like me to connect to his machine using my remote desktop support service, and he agreed.

How to Fix: Run Batch Script as Administrator (Without Password)

I have plenty of experience with batch scripts (.BAT, or .CMD), etc - however I have not come across the same type of problem where the script works using an administrative command line and then doesn't work when running it using Task Scheduler, even with Administrator access check marked.

After a bit of research I came across two solutions when it comes to running a program with administrative privileges using a batch script.

Solution #1: Run the Program in C:\Windows\System32

As many users have pointed out in online forums: if you place an executable inside of the C:\Windows\System32 directory, it should run with administrative privileges because that is where most of the .EXE files for the Windows operating system are stored.

Please note, however, this is a "hack," and assumes that the .EXE can run on its own outside of its normal installation directory, which might work for a very small percentage of programs. I do not recommend this method as this will have limited success and is also a security risk. Therefore, I recommend using Solution #2 below.

Solution #2: Set up the Task in Task Scheduler, then Call the Task Using a Batch File

To launch a program using administrative privileges (using a batch script), you must first set up the program inside of Task Scheduler - using administrative privileges. Once that is done you can call the program inside the batch script, which references the Task Scheduler entry.

As an example, these are the steps you would take if you are attempting to launch Thunderbird from a batch script (using a Task Scheduler entry):

  1. Launch Task Scheduler: click Start then type in "task scheduler" (no quotes); wait for Task Scheduler to appear and click it.
     
  2. At the top left of the Task Scheduler window, left click the "Task Scheduler Library" icon to highlight it, then right click over top of the highlighted area and select "Create Task" (do NOT select "Create Basic Task").
     
  3. The "Create Task" window will appear. Under the "General" tab, type in the name of the task - example: "Run Thunderbird", then click the option near the bottom that says "Run with Highest Privileges". At this point Windows should prompt you to enter your user name and password - this is a security protocol used to launch administrative tasks. Note: the user you are running the task under should have administrative rights, or the task won't run with administrative privileges.
     
  4. Normally when an entry is added to Task Scheduler, a "Trigger" is needed as part of automating the process of launching a task. That said, we are going to be calling this event from a batch script, so we will skip the "Trigger" tab in this case.
     
  5. Next, click the "Actions" tab of the task, then click the "New..." button near the bottom. A new window will appear; under the "Settings" heading, click the "Browse" button and select the executable for the program you wish to launch with administrative privileges from your batch script. In this case, we are using the Thunderbird.exe (executable) located in C:\Program Files\Mozilla Thunderbird\Thunderbird.exe. Once you have selected the .EXE file, click the "Open" button to add it, then click "OK".
     
  6. Finally, click the "Settings" tab, then un-check mark the option that says "Stop the task if it runs longer than:" (3 days). Click OK to add the task to Task Scheduler.
     
  7. Test to make sure that the task runs. Right click the Task Scheduler event you just created, and select "Run" from the dialogue menu. The task should run without any issues.

Continue on with the section below.

Call the Task Using Your Batch Script

If the task (event) is launching properly from the Task Scheduler main window, it's now time to call the Task Scheduler event using your batch script.

To do so, use the following syntax inside your batch file:

SCHTASKS /Run /TN "run thunderbird"

... Where "run thunderbird" is the name of the task as outlined in Step #3 in the previous section.

Continue on with the section below.

Launch Your Batch Script using Task Scheduler

You can now repeat all the steps above - this time, create a new even by adding your batch script to Task Scheduler.

This time you will want to use a "Trigger" to run the task every 24 hours (for example). Once the event has been added into Task Scheduler, you can test to see if it is working by right clicking over top of the event, then select "Run". Thunderbird (or whichever task you wanted to run with administrative privileges) should launch properly!

I hope that helps.

Additional 1-on-1 Support: From Dennis

If all of this is over your head, or if you need help automating a script / program and it needs to run with administrative privileges, I can help using my remote desktop support service. Simply contact me, briefly describing the issue and I will get back to you as soon as possible.

Got a Computer Question or Problem? Ask Dennis!

I need more computer questions. If you have a computer question -- or even a computer problem that needs fixing -- please email me with your question so that I can write more articles like this one. I can't promise I'll respond to all the messages I receive (depending on the volume), but I'll do my best.

About the author: Dennis Faas is the owner and operator of Infopackets.com. With over 30 years of computing experience, Dennis' areas of expertise are a broad range and include PC hardware, Microsoft Windows, Linux, network administration, and virtualization. Dennis holds a Bachelors degree in Computer Science (1999) and has authored 6 books on the topics of MS Windows and PC Security. If you like the advice you received on this page, please up-vote / Like this page and share it with friends. For technical support inquiries, Dennis can be reached via Live chat online this site using the Zopim Chat service (currently located at the bottom left of the screen); optionally, you can contact Dennis through the website contact form.

Rate this article: 
Average: 5 (5 votes)

Comments

zorge_12238's picture

This is very interesting technique. However, in my case in order to run schtasks the command prompt window should be ran as admin. Otherwise I get "ERROR: Access is denied." Which defeats the purpose... Any suggestions?
Thanks!

Dennis Faas's picture

Step #3 states to run the script with admin privileges. If this is not set then the whole thing won't run with administrator rights and the script will fail.

If you tested the batch file using the command line then you will have to run the command line with admin privileges as well, though that has nothing to do with Step #3. Running anything in an administrative command line would be for demonstrative purposes only (for yourself to see any error messages associated with the batch file) - but as stated, Step #3 would set the batch with admin rights under Task Scheduler.