1

I am trying to create a bat file that will run cmd as administrator, then execute net command as shown below:

runas /user:administrator cmd "net localgroup administrators mjordan /add"

The goal is to add whoever the current user is as a local administrator.

So the first part works like a charm, and asks for the admin password. Upon entering the admin PW however, I do not see said test user under the local admin group. My best guess is I made a syntax error. But oddly enough no errors show up and command-line exits as if it executed.

Also, how would I make this execute and add the current user to the local admin group (thinking in a %username% way)? Not sure I am using the proper terminology, but say the user logged on running this command is JSmith. How could I make it add without using his name so it works on any account rather than just JSmith

Do you guys notice any errors? ...I am fairly new to creating bat files, and am learning as much as I can, so i know i must have messed up somewhere. Also, any references or study help is appreciated! Thank you!

Sh3perd
  • 69
  • 6
  • 1
    I would try: `runas /user:administrator cmd /C "net localgroup administrators mjordan /add"` – Squashman Oct 05 '16 at 23:16
  • Quotes are in the wrong place as well. `runas /user:administrator "cmd /c net localgroup administrators %username% /add"` will get the current user as that is who is running the command. –  Oct 06 '16 at 01:19
  • For help see this link for help on symbols http://stackoverflow.com/questions/31820569/trouble-with-renaming-folders-and-sub-folders-using-batch. Type `help` then `command /?`. For the `net` command brief help `net /?` and `net localgroup /?` - full help is `net help localgroup`. So type `runas /?` and `cmd /?`. CMD is not needed in your command. So `runas /user:administrator "net localgroup administrators %username% /add"`. –  Oct 06 '16 at 01:22
  • @Noodles was spot on. That worked like a charm. And thank you for the help and advice. Greatly appreciated. – Sh3perd Oct 06 '16 at 14:16

0 Answers0