Windows : How to run a program with a different user (equivalent of linux 'sudo -u user cmd')?

Solution 1:

RunAs has to start another shell, because it needs to switch the attached permissions token of the shell, which otherwise is not possible with Windows API.

Answers:

Is there a way to wait for the program executed by RunAs to exit, and to capture its output ?

See this answer for how to wait for a program to terminate.

To capture its output it needs to be directed to a file, or if you were willing to study PowerShell, use the Tee-Object command (abbreviable to tee).

Is there a way to provide the credentials for the user in a non interactive way, or to bluntly say "I'm an admin, don't ask for the password" ?

You always need to specify the password. However, to avoid it being asked interactively, you may use instead of RunAs the free PsExec which also allows specifying the password on the command line (not a secure practice).

Do I have to run some checks on the host system, to see if RunAs is available ?

RunAs is part of Windows so is always available.