Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

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.

share|improve this answer
    
It didn't work for me with the double-quotes. –  Ghodmode Feb 14 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 at 4:52
add comment

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|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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