Search code examples
windowsbatch-filecmdrunas

Can't run batch file from runas command


I'm trying to open a file using runas command from cmd.

I succeed when i run it like this:

file.bat

or like this:

start file.bat

but when i run:

runas /user:username file.bat

the file opens but closes immediately (I'm entering the right password). I know that for sure that I'm entering the right password and that the batch file opens and closes without executing its content.

Any help please?


Solution

  • I don't know how to get the runas command to run in the same window. But a trick is to spawn the cmd window yourself with the /k switch to prevent it from closing when done:

    runas /user:username "cmd /k {fullpath}\file.bat"

    Note that you need the full path to the file because the new window opens to your user directory.

    Or you just put pause at the end of your .bat file instead of doing the above.