Runas Password in Batchfile

lucas220700 picture lucas220700 · Jun 17, 2019 · Viewed 13k times · Source

i want to run a batchfile with a runas command in it. Is it possible to link the password in Batch file? For example: runas /profile /user\domain PASSWORD thanks in advance for your answers

Answer

TripeHound picture TripeHound · Jun 17, 2019

An Alternate Solution

It looks as though runas does not allow what you want (without severely compromising security... see below). However, there is a third-party utility called PsExec which does allow this. From the Sysinternals PsExec page:

Usage: psexec [\\computer[,computer2[,...] | @file\]][-u user [-p psswd][-n s]
              [-r servicename][-h][-l][-s|-e][-x][-i [session]]
              [-c executable [-f|-v]][-w directory][-d]
              [-<priority>][-a n,n,...] cmd [arguments]

(Note: the idea to use PsExec came from Lauren7060's answer to this question on Spiceworks).

So, something like:

psexec -u MYUSER -p MYPASSWORD MYBATCH.BAT

should do what you want.


What Doesn't Work

As you found, runas normally prompts the user to enter the password. Interestingly, you can either pipe data into runas:

echo password | runas ...

or redirect input from a file:

runas ... < PASSWORD.TXT

and runas will not prompt for a password. However, I could not get runas to accept the password as valid, however I formatted the data. In the latter case (redirecting from a file), I tried both with and without a line-ending, but neither worked. My guess is that the pipe/redirect is enough to stop runas prompting for a password, but that it doesn't really read anything from stdin. This is partially supported by this article on the Tech Torials website that describes a method where you can use runas without it prompting for a password, but only if you set the account's password to an empty string.