How to create a hosts file shortcut with administrator privileges?

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.


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.
  • 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