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:
- Click on the start menu
- Type
notepad
- Right-click on the notepad icon, click Run as administrator
- Click Yes
- 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.
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:
There you are, you will need to confirm with UAC each time you use this.
Right click on the desktop
New > Shortcut
type "notepad" as the location
Give the shortcut a name
Right click -> Properties on the shortcut
to the end of the "target" text box, append "%SYSTEMROOT%\System32\drivers\etc\hosts" (remember to leave a space)
Click on "Advanced"
Check Run as Administrator
then press OK, on both windows
-
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.