0

I am logged on as user and want to "runas" to run some command script as administrator.

This does not work: ">runas /user:administrator mybatch.cmd" : mybatch.cmd executes but is not running as administrator.

Or "runas /user:administrator cmd.exe" - it does run a command prompt, it says "running as administrator" - but in reality it is not.

I can right-click mybatch.cmd and "Run as administrator" : and that works fine.

It seems there is a great difference between "User Account Control" dialog prompting for administrator and "runas /user:administrator".

I have turned user-control to 0 and restarted, just in case, it made no difference.

  • 1
    If you open CMD.EXE with Run as Administrator, it runs as Administrator. I have not seen anything different. Is mybatch trying to run in a secured location? That may cause what you see. – John Nov 30 '22 at 17:03
  • Of the quoted "Closed: Duplicate - Is answered here", the first there is no accepted answer and of the others there is no concise answer. There is a lot of confustion between run elevated and (as per this question" REALLY run as local Administrator. In fact you could argue the one answer here is one of the better ones. This is a good example of why SE's QA policies are broken :( – Jay M May 03 '23 at 12:45

1 Answers1

0

That RunAs won't work indeed; it says so in its help:

RunAs is not able to launch an application with an elevated access token.

So you're only telling it to run as the account "Administrator"; it will run your program from the Administrator account (using its settings, environmental variables, etc.) but indeed without administrative rights, just as running a program from any account in the Administrators group would. The program needs to ask for administrator permission to be given.

What exactly do you want to do? If you simply want to manually run a batch file as administrator without having to right-click it, you can create a shortcut of it, right-click the shortcut, choose Properties on the context menu, go to the Shortcut tab, and check "Run as administrator". Then, after clicking Ok, every time you run that shortcut it will run as administrator.

If you want it, for example, to run every time you login, you could use a Scheduled Task, as Fazer87 explains here:

  1. Open Task Scheduler

  2. Create a new task

  3. In the "General" tab - ensure the following settings are entered:

  • "Run whether user is logged on or not"

  • "Run with highest privileges"

  • "Configure For" (your operating system)

  1. In the "Triggers" tab, when adding a trigger (schedule) - ensure that the "Enabled" checkbox is checked

To run from a command, you can use RunAs, but there are more convenient tools such as WinAero's "Ele" that will help you. You just drop it in whatever path is in your %path% (maybe C:\Windows\system32) and then you will be able to use the command "ele myfile.bat".

Finally, you can launch an elevated program with PowerShell through the Start-Process command. As explained here, on an unelevated PowerShell window you can type:

Start-Process powershell -ArgumentList '-noprofile -file MyScript.ps1' -verb RunAs
Ramhound
  • 41,734
  • 35
  • 103
  • 130
  • 1
    I want to avoid having to type in an admin password every time I want to run a script. Otherwise, yes, I can right click and "run as administrator" as I already mentioned, or start a cmd.exe as administrator, and then run my other scripts from there. – Shares Games Nov 30 '22 at 23:12
  • 1
    You can utilize psexec: when used with the built-in user "administrator", it will start things elevated. It accepts username and pw in Plaintext. – Bernd Schwanenmeister Dec 01 '22 at 14:13