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
-
What do you mean by `link`? – SNR Jun 17 '19 at 09:56
-
maybe check out `net use` [here](https://ss64.com/nt/net-use.html). – mael' Jun 17 '19 at 12:39
-
Read `runas /?` (there is no _password_ parameter in the command line). _Enter user's password only when prompted._ – JosefZ Jun 17 '19 at 13:46
2 Answers
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.

- 2,721
- 23
- 37
-
*N. B.:* `echo password | runas ...` would write `password` + _space_ into the pipe… – aschipfl Mar 14 '22 at 13:50
-
@aschipfl Aware of that, and there are ways of avoiding adding the space when needed. The problen is/was that `runas` wasn't reading from the pipe. Reflecting on things while typing this reply, my suspicion is that it flushes `stdin` (a not unreasonable thing to do if it's connected to a keyboard, to prevent erroneous typeahead), thus consuming and discarding anything piped-in. – TripeHound Mar 14 '22 at 13:58
I have also finding the way how to do this.But unfortunately i cant find any way how to do this on internet.Then i discovered(from own) one way from which you can do it without using any third party software.You can do this by using batch scripting and vbs. First I have created a vbs file to send password to command prompt and press enter automatically.I have pause script for 1s so that password can be entered after running runas command. Simply Create a file like this:-
::Creating vbs file to give password to runas command:-
echo Set x=CreateObject("wscript.shell")>C:\users\%username%\Desktop\pass.vbs
echo wscript.sleep 1000>>C:\users\%username%\Desktop\pass.vbs
echo x.sendkeys "<password of user>">>C:\users\%username%\Desktop\pass.vbs
echo x.sendkeys "{enter}">>C:\users\%username%\Desktop\pass.vbs
start C:\users\%username%\Desktop\pass.vbs
runas /user:<Username for which you want> "<File that you want to run>"

- 11
- 2