Windows – Runas different user to launch CMD and run command

command linewindows

I want to run a batch file as a different user, via the Windows -> Run dialogue.

I have tried:

runas /profile /user:test CMD "C:\Users\MyName\Desktop\Run.bat"

runas /profile /user:test "CMD "C:\Users\MyName\Desktop\Run.bat""

runas /profile /user:test "CMD \"C:\Users\MyName\Desktop\Run.bat\""

runas /profile /user:test "CMD \"C:\Users\MyName\Desktop\Run.bat"\"

but still no luck. It opens up CMD and asks me for my password. I enter it, and it opens another CMD window using the other username, but it doesn't run the bat. Instead, the title bar just says

Administrator: cmd "C:\Users\MyName\Desktop\Run.bat" (running as Home-PC\test)

Can anyone please help?

Best Answer

You're missing a switch. From CMD /?:

/C      Carries out the command specified by string and then terminates
/K      Carries out the command specified by string but remains

These commands should work. Choose the one that suits your needs.

runas /profile /user:test "CMD /C C:\Users\MyName\Desktop\Run.bat"

runas /profile /user:test "CMD /K C:\Users\MyName\Desktop\Run.bat"

If the path contains spaces, surround it with escaped double quotes (\"):

runas /profile /user:test "CMD /C \"C:\Users\My Name\Desktop\Run.bat\""

runas /profile /user:test "CMD /K \"C:\Users\My Name\Desktop\Run.bat\""
Related Question