How do I run a program from command prompt as a different user and as an admin [closed]
Asked Answered
B

9

61

I am using "runas" to open command prompt as a different user but that command prompt is not running as an admin. How can I make it run as an admin?

UPDATE: I am using Windows Server 2012

UPDATE: I opened cmd for another account by running

 runas /user:domain\username cmd.exe

Then I tried to run some commands in this new prompt but this is not running as an elevated user (even though it has Administrator privileges).

Beker answered 15/10, 2012 at 20:39 Comment(2)
I couldn't make it work with runas /user:domain\username cmd.exe I had to run it like this: runas /netonly /user:domain\username cmd.exeHypotenuse
Thanks for your comment! /netonly did the trick for me tooMercaptopurine
G
70

See here: https://superuser.com/questions/42537/is-there-any-sudo-command-for-windows

According to that the command looks like this for admin:

 runas /noprofile /user:Administrator cmd
Gunslinger answered 15/10, 2012 at 20:44 Comment(2)
Why does it require me to enter a password when I can right click any other program and run as admin without entering a password? ThanksLeibowitz
because in this case you are running a program on behalf of another user as opposed to the case when you are already in the admin group and you run the program yourself but with elevated rights.Analysand
G
17

(Win 10) Run as another user:

Start Menu, ctrl + shift click : Cmd Prompt

Gladygladys answered 1/4, 2013 at 19:42 Comment(3)
For me this works: Shift + Ctrl while clicking the programThermostat
though it is a way to run a program as admin, but it is not the answer to the original question.Fern
How execute as another user AND Administrator ?Pazia
P
16

All of these answers unfortunately miss the point.

There are 2 security context nuances here, and we need them to overlap. - "Run as administrator" - changing your execution level on your local machine - "Run as different user" - selects what user credentials you run the process under.

When UAC is enabled on a workstation, there are processes which refuse to run unless elevated - simply being a member of the local "Administrators" group isn't enough. If your requirement also dictates that you use alternate credentials to those you are signed in with, we need a method to invoke the process both as the alternate credentials AND elevated.

What I found can be used, though a bit of a hassle, is:

  • run a CMD prompt as administrator
  • use the Sysinternals psexec utility as follows:

    psexec \\localworkstation -h -i -u domain\otheruser exetorun.exe

The first elevation is needed to be able to push the psexec service. The -h runs the new "remote" (local) process elevated, and -i lets it interact with the desktop.

Perhaps there are easier ways than this?

Psychopharmacology answered 5/11, 2013 at 19:28 Comment(1)
psexec may not be available...Cozy
N
14

I've found a way to do this with a single line:

runas /user:DOMAIN\USER2 /savecred "powershell -c start-process -FilePath \"'C:\\PATH\\TO\\YOUR\\EXECUTABLE.EXE'\" -verb runAs"

There are a few tricks going on here.

1: We are telling CMD just to run Powershell as DOMAIN\USER2

2: We are passing the "Start-Process" command to Powershell, using the verb "runAs" to elevate DOMAIN\USER2 to Administrator/Elevated privilege mode.

As a general note, the escape characters in the "FilePath" argument must be present (in other words, the "\ & \\ character combinations), and the single quotation (') must surround the EXE path - this way, CMD interprets the FilePath as a single string, then Powershell uses the single quotation to interpret the FilePath as a single argument.

Using the "RunAs" verb to elevate within Powershell: http://ss64.com/ps/syntax-elevate.html

Natal answered 17/10, 2016 at 23:40 Comment(1)
In Windows 2010 this is the only solution that worked for me.Daemon
T
4

You can use psexec.exe from Microsoft Sysinternals Suite https://learn.microsoft.com/en-us/sysinternals/downloads/sysinternals-suite

Example:

c:\somedir\psexec.exe -u domain\user -p password cmd.exe
Tibiotarsus answered 1/6, 2018 at 10:0 Comment(1)
psexec is dang awesomeBolivar
H
1

Runas doesn't magically run commands as an administrator, it runs them as whatever account you provide credentials for. If it's not an administrator account, runas doesn't care.

Huggermugger answered 15/10, 2012 at 20:44 Comment(7)
The account I am opening a command prompt in is an Administrator account.Beker
One potential gotcha is that depending on how you invoke runas, you may inherit environment variables from the existing user session, so it's possible that things like echo %USERNAME% may not report that you're the other user. What are you trying to do with this admin prompt?Huggermugger
If you run your admin prompt and run the command whoami do you get the user you expect?Huggermugger
What are you trying to do with it that requires administrative rights that is not working?Huggermugger
I am trying to run an exe which requires admin privilegesBeker
What error message are you getting, specifically?Huggermugger
Administrator privileges not found. The account does have administrator privileges (grouped under "Administrators").Beker
W
1

In my case I was already logged in as a local administrator and I needed to run CMD as a domain admin so what worked for me was running the below from a powershell window:

runas /noprofile /user:DOMAIN\USER "cmd"

Worldshaking answered 27/6, 2018 at 19:32 Comment(0)
G
1

Open notepad and paste this code:

@echo off
powershell -Command "Start-Process cmd -Verb RunAs -ArgumentList '/c %*'"
@echo on

Then, save the file as sudo.cmd. Copy this file and paste it at C:\Windows\System32 or add the path where sudo.cmd is to your PATH Environment Variable.

When you open command prompt, you can now run something like sudo start ..

If you want the terminal window to stay open when you run the command, change the code in notepad to this:

@echo off
powershell -Command "Start-Process cmd -Verb RunAs -ArgumentList '/k %*'"
@echo on

Explanation:

powershell -Command runs a powershell command.

Start-Process is a powershell command that starts a process, in this case, command prompt.

-Verb RunAs runs the command as admin.

-Argument-List runs the command with arguments.

Our arguments are '/c %*'. %* means all arguments, so if you did sudo foo bar, it would run in command prompt foo bar because the parameters are foo and bar, and %* returns foo bar.

The /c is a cmd parameter for closing the window after the command is finished, and the /k is a cmd parameter for keeping the window open.

Gena answered 12/4, 2019 at 2:28 Comment(2)
This does elevate (thanks to -Verb RunAs), but doesn't do the 'run as a different user' part...Knipe
This seems highly unreliable as it depends on the command prompt finding such text file, the subarguments to powershell remaining stable, escaping details, etc.Thaine
C
-3

The easiest is to create a batch file (.bat) and run that as administrator.

Right click and 'Run as administrator'

Camail answered 9/8, 2013 at 16:40 Comment(1)
The OP is running as a normal user NOT an unelevated adminInbreeding

© 2022 - 2024 — McMap. All rights reserved.