Self-Elevating to make hosts file change in Windows

Working on a Windows machine without elevated permissions can often be difficult for developers. One item that is often useful to change is the ‘hosts’ file. IN Windows 7 and 8 you can often ‘Self-Elevate’ to run a file, but it’s not always obvious how to edit a file in this manner. Some simple batch files can be helpful in this case as you can elevate them to do the actual work requiring permissions.

For example to make all requests to ‘’ to be directed to your own machine…

@echo off
set hostspath=%windir%\System32\drivers\etc\hosts
echo >> %hostspath%
echo >> %hostspath%

To replace the existing hosts file with one of your chosing from your desktop. (NOTE: you can change this file or path to anything).

copy "%UserProfile%\Desktop\hosts" "c:\Windows\System32\drivers\etc"

A standard ‘hosts’ file in Windows appears as such:

# Copyright (c) 1993-2009 Microsoft Corp.
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
# For example:
# # source server
# # x client host

# localhost name resolution is handled within DNS itself.
# localhost
# ::1 localhost

Leave a Reply