16

As a developer, I find myself editing the hosts file quite often to add a local virtual host.

As the hosts file needs to be edited as an administrator, I have to:

  1. Click on the start menu
  2. Type notepad
  3. Right-click on the notepad icon, click Run as administrator
  4. Click Yes
  5. Then Open... C:\Windows\System32\drivers\etc\hosts

Quite cumbersome.

I've tried to create a shortcut to the hosts file on the desktop, then right-click it to change the shortcut advanced properties, but I can't select Run as administrator.

Shortcut advanced properties

I guess this is because I'm creating a shortcut to a file, and not to the notepad program.

How can I create a shortcut to notepad that runs as an administrator and opens the hosts file? It's OK if I have to click Yes every time to confirm that I want to run the program as an administrator.

35

Here you go:

  1. Right click on the desktop

  2. New > Shortcut

  3. type "notepad" as the location

  4. Give the shortcut a name

  5. Right click -> Properties on the shortcut

  6. to the end of the "target" text box, append "%SYSTEMROOT%\System32\drivers\etc\hosts" (remember to leave a space)

  7. Click on "Advanced"

  8. Check Run as Administrator

  9. then press OK, on both windows

There you are, you will need to confirm with UAC each time you use this.

  • 1
    Works, thanks! Missing a backslash after %SYSTEMROOT% though, and I can't edit your post without editing at least 6 chars. – Benjamin Jul 29 '13 at 20:22
  • @Benjamin got it on my end!, thanks for letting me know – KJ4IPS Jul 29 '13 at 20:25
  • 1
    Works with VSCode with target: "C:\Program Files\VSCode\Code.exe" %SYSTEMROOT%\System32\drivers\etc\hosts – Nick Tsai May 19 '18 at 7:02
5
  • CMD batch without UAC confirmation

    Open a new text file, copy&paste the line and save it as something.cmd

    runas /profile /user:Administrator /savecred "notepad C:\Windows\System32\drivers\etc\hosts"
    
  • Shortcut without UAC confirmation

    Right-click on desktop » New » Shortcut

    cmd /c runas /profile /user:Administrator /savecred "notepad C:\Windows\System32\drivers\etc\hosts"
    

Both methods will ask for the administrator password first time and save it.
This only works if your administrator account is enabled.

enter image description here

  • Elegant solution, but: 1/ I don't understand what you mean with Open a new text file, did you mean New shortcut? 2/ I don't use the Administrator account unfortunately, and indeed this doesn't work with my user account (even though I have the privileges to run as administrator). – Benjamin Jul 29 '13 at 20:16
  • Edited my answer for clarification – nixda Jul 29 '13 at 20:22
  • 1
    Thanks! Best solution if you have the Administrator account enabled. – Benjamin Jul 29 '13 at 20:25
3

Create a folder with full permissions for everyone and add it to PATH.

Inside it create a file called sudo.cmd with following text inside:

@echo Set objShell = CreateObject("Shell.Application") > %temp%\sudo.tmp.vbs
@echo args = Right("%*", (Len("%*") - Len("%1"))) >> %temp%\sudo.tmp.vbs
@echo objShell.ShellExecute "%1", args, "", "runas" >> %temp%\sudo.tmp.vbs
@cscript %temp%\sudo.tmp.vbs

Now, typing sudo notepad will start Notepad with Administrator privileges (with UAC prompt first). It's really handy (you can also sudo cmd or sudo AnyAppYouCanThinkOf).

Then create a file called hosts.cmd and save it with following line:

@sudo notepad %systemroot%\System32\drivers\etc\hosts

Now just press Win and type hosts, then press Enter and you have an UAC prompt to open the hosts file in Notepad!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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