I'm trying to run
xcopy commands in a batch file as an administrator. Is this the correct command and how can I pass the password into it?
runas /noprofile /user:Domain Name\user account
To automatically authenticate, add the
/savecred flag. You'll have to enter the password on the script's first run, but it will be saved after that.
runas will then be able to use the saved credentials to execute any given program, so do think about how that could be a security issue before going this route.
Your usage is correct though, just add the program path at the end.
runas /noprofile /user:email@example.com /savecred script.bat
runas /noprofile /user:joe-pc\joe /savecred script.bat
To delete a saved credential search for the Credential Manager in Control Panel.
runas command should prompt you for the credentials when you run the batch file to execute the specified command. As long as you run it in cmd.exe. This is due to the need for the standard input neccessary to prompt for the password. You cannot just double click it. Storing a password in plaintext in a batch file is a bad idea because it's insecure. That's why it prompts at runtime.
You may right click on the executable and click
Run as Administrator, without the
runas command. This is probably the solution you are looking for.
If you wanted to run the task as a scheduled task, you can set it to run it as different user there.
You could use RunAs when you call the batchfile (right click on the batchfile and click runas) to have the batch run as administrator. I suspect that runas will not work inside the batch. I'd recommend NOT putting the password in the batch file, even if it did work, batch files are plain text, so you might as well put the password on a post-it note on the monitor.
This website discusses creating an autoit script that obfuscates the the contents of the file towards the bottom of the file.