Windows CMD Batch File Run A Command as Administrator

cytinus picture cytinus · Jul 10, 2012 · Viewed 23.6k times · Source

I have a batch file where I want to run some commands, but not all of them as administrator. I do not want to run the command prompt as administrator or run the entire batch job as administrator. An example of a command that I want to run as administrator is:

appcmd.exe recycle apppool \apppool.name -DefaultApp

Appcmd is the command for managing IIS 7, and this example I want to recycle an App Pool.
I have tried:

runas /profile /user:MYPC\administrator "appcmd.exe recycle apppool \apppool.name -DefaultApp"

I get a 1326: Logon failure: unknown user or bad password, but I know the user and password exist. Any suggestions as to how I can do this?

Answer

Geoffrey McGrath picture Geoffrey McGrath · Jul 26, 2012

I found that running runas without the /profile parameter worked fine. But if you must run it with /profile then you could execute this command just prior:

runas /profile /user:MYPC\administrator "cmd"

Which will unfortunately prompt you twice.