12

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.

3 Answers 3

8

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.

4
  • It didn't work for me with the double-quotes.
    – Vince
    Feb 14, 2014 at 3:40
  • @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)
    – Bob
    Feb 14, 2014 at 4:52
  • Note that the powershell process does not have to be run elevated in order to run a program as administrator.
    – jiggunjer
    Jan 17, 2016 at 13:09
  • Strange that it was working before a Windows 10 2019-03 update.
    – Qwerty
    Apr 21, 2019 at 19:18
13

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.

1
  • 1
    My administrator user stopped working after Windows 10 2019-03 update, this command fixed it net user administrator /active:yes
    – Qwerty
    Apr 21, 2019 at 19:19
-1

One thing that you could try is opening command prompt as admin and running notepad through there. Just type Notepad. After you do that from there you can click file then open and navigate to the hosts file and open it. Finally, you can just save like normal.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .