Run as administrator from command line on Windows 8

20,457

Solution 1

Ok, the reason this doesn't work is the security model in Windows Vista and newer. An account in the administrators group still runs everything not explicitly elevated as a limited user. The exception is the Administrator account, which runs everything elevated. For this reason, it is considered generally bad to use as your login account, and is normally disabled.

You could enable it and then runas to invoke as that account. That introduces a few problems - now you're running with the environment of a different user, which could have different environment variables set.1

The better way to do this would be actually elevate as your current user via UAC. Unfortunately, the standard command prompt doesn't include that capability - but both third party programs and the built-in PowerShell and WSHell (VBScript) can do so.


Borrowing from my other answer, you can invoke the PowerShell command directly with powershell -c:

powershell -c start -verb runas notepad C:\Windows\System32\drivers\etc\hosts

which basically tells PowerShell to run the following (start is aliased to Start-Process):

Start-Process -Verb "runas" notepad C:\Windows\System32\drivers\etc\hosts

The trick here is passing the verb runas, triggering UAC.

Neither Start-Process -Verb runas nor the standard cmd runas will pass the current working directory, so always use the full path in any commands you elevate in this fashion.

Also note that some arguments like -c may clash with Start-Process arguments, so the safest way is:

powershell "-c start -verb runas commandname -argumentlist 'arg1 arg2'"

1 Note: this only applies to the user's environment variables. Environment variables you set in a parent process are not passed on by UAC! This also applies to runas, and it's even worse there because you won't even get the correct user's vars.

Solution 2

Most likely, you haven't yet enabled the administrator account.

Here are instructions for enabling the administrator account.

You'll also find more info on runas on the Microsoft site.

Share:
20,457

Related videos on Youtube

Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    Admin over 1 year

    I am trying to run Notepad as admin so I can edit my hosts file from the command line.

    I have tried runas /user:(myusername)\administrator "notepad c:\windows\system32\drivers\etc\hosts" I then input my password and I get

    RUNAS ERROR: Unable to run - notepad c:\windows\system32\drivers\etc\hosts 1327: Account restrictions are preventing this user from signing in. For example: blank pa sswords aren't allowed, sign-in times are limited, or a policy restriction has been enforc ed.

    PS: I know if I give permission to my user account I can edit it without running as admin. But I'd like to know how to do this without having to change permissions on the hosts file.

  • Vince
    Vince about 10 years
    It didn't work for me with the double-quotes.
  • tvdo
    tvdo about 10 years
    @Ghodmode Could you give an example of the line you tried to run? If you're referring to the last example, try moving the first quote to after the -c. (so -c "start instead of "-c start)
  • jiggunjer
    jiggunjer over 8 years
    Note that the powershell process does not have to be run elevated in order to run a program as administrator.
  • Qwerty
    Qwerty about 5 years
    Strange that it was working before a Windows 10 2019-03 update.
  • Qwerty
    Qwerty about 5 years
    My administrator user stopped working after Windows 10 2019-03 update, this command fixed it net user administrator /active:yes