-1

I'm looking for some suggestions (or snippets) on how to elevate a command prompt session as a local admin and run a program within this elevated session. Does anyone have any idea or know how? Is this possible to do using a single .bat file? For example, open a command window as admin and add a user to local user group.

Thank you

  • does right click not work? (on start menu or batch file) – Smock Sep 10 at 14:56
  • 2
    Use the RUNAS command? – harrymc Sep 10 at 16:44
1

Use the run box (windows key +r). Enter a command (like cmd). hold ctrl+shift keys and press enter. Alt + y to accept the UAC prompt. Elevated command prompt in 10 key strokes.

If you put a batch file in a folder on your systems PATH you can execute it directly from the run box. Eg create c:\windows\mycoolscript.bat, and from the run box just enter mycoolscript (a better idea is to modify the path to include the folder where you scripts live - either works)

To add a user to a group look at the net command:- https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2012-r2-and-2012/cc754051(v%3Dws.11)

Updated following comments...

If you want an unprivileged user to run privileged commands from a script you need to provide them credentials, or have a more privileged account execute the script on the users behalf.

Providing credentials - PowerShell can generate "SecureStrings", and you can store a representation of that object in a file and reload it later so you could . In theory the file can only be read back on that machine, but it uses reversible encryption so its not really that secure - but its easy and might be good enough? Eg https://4sysops.com/archives/encrypt-a-password-with-powershell/

Another option is to take advantage of Windows Task Scheduler - setup your ps2 script with an account with whatever rights the script requires and a trigger based on a Windows Event Log event. This allows your unprivileged ps1 user to write a message to the Windows Event Log (using custom event id), and have your ps2 script be executed in response to that custom id under different credentials. This is more secure, but theres quite a bit more work involved.

  • Basically I have two powershell commands and let's called them ps1 & ps2. ps1 is to elevate the existing cmd prompt to local admin and obviously this will popup a new cmd window. In the new window, I run ps2. This works as long as I run them manually. If I put ps1 & ps2 inside myfile.bat, i don't see the user added. – sydney Sep 11 at 13:48
  • @sydney please edit your question and include these details. Also, is the user executing ps1 an administrator or not? – MisterSmith Sep 11 at 21:45
  • User executing ps1 is not a local admin. – sydney Sep 12 at 12:20
  • I did some testing. When I use "runas" to open a command prompt as a domain\user who is a member of the local admin group on a domain computer. The new command prompt it opens ISN'T the same as running cmd.exe "Run as administrator" from the Start - Menu. That's my problem. How do I fix this? – sydney Sep 12 at 15:19
  • The problem I have is similar what this person posted - stackoverflow.com/questions/12903629/… – sydney Sep 12 at 15:20
0

I do this:
{START}
scroll down to Windows System, expand it with a click
Right-click on Command Prompt, choose Run as administrator and once the Command Prompt window is opened, launch the script.

If you want to elevate an existing Command Prompt, use RUNAS to execute the script. That man page for RUNAS provides examples as well as options and switches available.

Or experiment with Elevate Command in Powertoys for Vista, see if it still works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.