How can I end a process with admin privileges using a batch file

Robert Buckley picture Robert Buckley · Jan 27, 2015 · Viewed 15.6k times · Source

I have written a batch file which starts OpenVPN. If the client is found, I check the connectivity by running PING. If the server is not found, then this means that although OpenVPN is running, the connection has not been established. At this point I want to restart OpenVPN. Because I am not using the admin account for this script, I always get the error "permission denied" when using TASKKILL /F. Is there anyway around this?

Answer

Monacraft picture Monacraft · Jan 27, 2015

There are two methods of running a command with administrator privileges.

Solution 1:

Right-Click the batch-file and click run as admin.

Solution 2:

Use the runas command to run a command or batch file with administrative permissions. This way the process is still automate but you will still need to type in the password for administrator.

Example:

C:\> runas /user:administrator "taskkill /f /im explorer.exe"
Enter the password for administrator:

Type runas /? for more info.