In this blog post I’ll show you a quick an easy way to create a shortcut to the HOSTS file on Windows 7/Server 2008+. By itself that wouldn’t be a huge deal, but I’ll throw in a nice bonus so that you’ll add the “Run as Administrator” property so you can save the edits you make. If you are like me and frequently need to edit the HOSTS file on a computer running with UAC enabled (Vista, Windows 7, Server 2008+) you may have run into the following error when trying to save changes:
“C:WindowsSystem32driversetchosts.txt
You don’t have permission to save in this location. Contact the administrator to obtain permission.
Would you like to save in the My Documents folder instead?”
The Issue
The error above is caused by the program you are using to edit the HOSTS (Notepad in my case) isn’t running in “Run as Administrator” mode while trying to modify a system file. As a result it will ask you to save to a location (My Documents) that it does have access to. Unfortunately we do need to save it to the original location.
Background on HOSTS file
For those unfamiliar, the HOSTS file is used to locally map hostnames to IP addresses and will supersede values that come from Domain Name System (DNS). As a developer I use the HOSTS file to point my local machine to a development environment that isn’t registered in DNS or perhaps a specific server in a load balanced server farm.
The Solutions
Yes you read this section title right, I came up with multiple solutions (2 1/2 really) to this issue. If you prefer to see me dazzle you with my PowerShell skills skip ahead to solutions #2 and #3. If you’d like the old-fashioned “by hand” solution check out #1 below.
Solution #1
As stated above, the first solution I would consider more of a manual approach. The first thing you will do is create a shortcut to your favorite text editing software. Since Notepad comes with just about every Windows OS I chose that for simplicity. One way to create the shortcut is to find it in the All Programs of your Start menu and Right Click –> Send To –> Desktop (create shortcut).
When calling Notepad from the command line it is possible to specify the file to open automatically by listing it as a command line argument. If your text editor doesn’t support this you’ll need to find an alternate approach. Once you have the created the shortcut edit the properties by Right Clicking the shortcut and choosing Properties. You will need to add the file location of HOSTS (%windir%system32driversetchosts) to the value already in the Target box shown below. After you have added the file location to Target click the Advanced button in the lower right shown below. On the advanced properties window you’ll want to check the box for “Run as administrator” which then allows you to edit the HOSTS file when opened in Notepad. Click OK.
If everything was successful when you double click the shortcut you should be prompted by a UAC box (because we chose to Run as Administrator) that lists the program (Notepad) and the file location (HOSTS file) to be opened. Click Yes.
Solution #2
The first PowerShell solution automates the process of creating the desktop shortcut but with one drawback: you still need to manually set the shortcut to “Run as Administrator”. Just follow the steps from Solution #1 to add that additional piece.
Download the script here.
$wshell = New-Object -comObject WScript.Shell
$desktopPath = $wshell.SpecialFolders.Item('Desktop')
$link = $wshell.CreateShortcut("$desktopPathHOSTS.lnk")
$link.TargetPath = '%windir%system32notepad.exe'
$link.Arguments = '%windir%system32driversetchosts'
$link.Description = 'launches HOSTS file'
$link.WorkingDirectory = '%HOMEDRIVE%%HOMEPATH%'
$link.IconLocation = '%windir%system32notepad.exe'
#$link.Hotkey = "CTRL+SHIFT+H"
$link.Save()
Note: You’ll notice that I commented out the line to assign a hotkey. I was using that hotkey as a 3-button quick test of my script results instead of multiple clicks to return to the desktop, find the shortcut, and click it. Feel free to uncomment and use it permanently, just be sure you don’t already have a hotkey mapped to that key combination.
Solution #3 (more like #2b)
Since I couldn’t find a way to automate the setting for “Run as Administrator” in Solution #2 I continued my pursuits by “thinking outside the box.” I thought if I can’t set that property perhaps I can launch the process in elevated mode instead. (Sidenote: anyone familiar with Mythbusters, one of my favorite TV shows, may recognize their philosophy of attempting to recreate the results without necessarily adhering to the exact circumstances originally proposed.) As a result what I ended up with was a shortcut on the desktop that actually runs a PowerShell command that in turn opens the HOSTS file in “Run as Administrator” mode.
Download the script here.
$wshell = New-Object -comObject WScript.Shell
$desktopPath = $wshell.SpecialFolders.Item('Desktop')
$link = $wshell.CreateShortcut("$desktopPathHOSTS.lnk")
$link.TargetPath = 'PowerShell'
$link.Arguments = '-command "Start-Process "notepad.exe" -Verb Runas -ArgumentList "C:windowssystem32driversetchosts""'
$link.Description = 'launches HOSTS file'
$link.WorkingDirectory = '%HOMEDRIVE%%HOMEPATH%'
$link.IconLocation = '%windir%system32notepad.exe'
#$link.Hotkey = "CTRL+SHIFT+H"
$link.Save()
While I do appreciate that I was able to get the results I wanted there are a few things that I don’t like about this solution. First is that I had to hardcode the “C:…” file location for HOSTS. While a large percentage of users will have their Windows folder on the C: drive, not all will (especially one of my home machines where Windows is on the W: drive.) Technically I could overcome this with enough string manipulations, but I didn’t have time to mess with it. The second issue is that I have an intermediate step of launching the PowerShell console which then runs the open Notepad command. The PowerShell console only shows for a second or two and uses minimal resources but it’s definitely not as efficient as it could be.
Conclusion
So there you have it, 3 different ways to quickly and easily launch the HOSTS file in a save-able manner. The first two solutions have a minor amount of manual steps required while the third is fully automated but with the drawbacks that I pointed out. If anyone reading this knows a way to assign the “Run as Administrator” property through PowerShell I would very much appreciate a link or resource. I spent a good chunk of time researching but came up empty handed. I hope you were able to gain something useful from this post. I know I had a great deal of fun researching for it.
-Frog Out
Links
Solution #2 and #3 script downloads
Create shortcut PowerShell script adapted from
http://powershell.com/cs/blogs/tips/archive/2009/04/20/create-powershell-shortcuts.aspx
Open with “Run as Admininistrator” PowerShell concept adapted from
Originally posted on: https://briantjackett.com/archive/2010/07/19/create-shortcut-to-easily-edit-hosts-file-on-windows-7.aspx#530695Neil, Sorry I wasn’t getting notifications about comments on my blog for past month it appears, so catching up. Glad you like the post. I like your suggestion about putting different environments commented out. I’m going to start doing that myself. Thanks. Brian
LikeLike
Originally posted on: https://briantjackett.com/archive/2010/07/19/create-shortcut-to-easily-edit-hosts-file-on-windows-7.aspx#551218You could use the $env:SystemRoot as %windir%
LikeLike
Originally posted on: https://briantjackett.com/archive/2010/07/19/create-shortcut-to-easily-edit-hosts-file-on-windows-7.aspx#551230AKrasheninnikov, Thanks for the suggestion. I’ve slowly been picking up on automatic variables like that over time and using in my newer scripts.
LikeLike
Originally posted on: https://briantjackett.com/archive/2010/07/19/create-shortcut-to-easily-edit-hosts-file-on-windows-7.aspx#625861This is the best! Thank you. I decided to start looking for and found this about 3 years later than I should have.
LikeLike