Deleting and Adding an Entry from the hosts file.

Posted on 2005-05-10
Last Modified: 2012-05-05
I need a batch file that will delete an entry from the hosts file. The hosts file that I have looks like this:

# Copyright (c) 1993-1999 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

#      b035
#     klse-b  server      c135       klse-c server        pamserver1
#        pamserver2

1. How can I delete an entry from the hosts file? Say I want to delete the entire entry for So how do I delete the entire line of entry?

2. Once I have deleted this line of entry, how do I add an additional line of entry say into the hosts file?

Thank You
Question by:ben1211
    LVL 95

    Accepted Solution

    REM The following will find all lines in the file hosts NOT containing ""
    REM This is done through the use of the /v switch.  The /i switch ignores CaSe.
    REM The end takes the found lines and directs them to the new file,
    find /v /i "" %windir%\system32\drivers\etc\hosts >
    REM Then we delete the existing hosts file
    del hosts
    REM Then we rename the new file to be THE hosts file.
    ren hosts
    REM To add an entry, simply do this:
    Echo >> %windir%\system32\drivers\etc\hosts
    REM the above line will append a line to the hosts file containing the information
    REM between the word Echo and the >> characters.
    LVL 10

    Expert Comment

    hosts file have normally the read only attribute.
    I will first create a backup in case of...

    copy %windir%\system32\drivers\etc\hosts %windir%\system32\drivers\etc\hosts.bak
    attrib -R %windir%\system32\drivers\etc\hosts
    Type %windir%\system32\drivers\etc\hosts.bak | find /V "The.IP.You.Want2Delete" > %windir%\system32\drivers\etc\hosts
    attrib +R %windir%\system32\drivers\etc\hosts
    LVL 26

    Expert Comment

    by:Leon Fester
    Why don't you just create the files that you need with the updated hosts information and then overwrite the existing file?

    LVL 95

    Expert Comment

    by:Lee W, MVP
    Longbow -

    Backup is always a good idea.  But hosts is NOT usually read only - unless you "protect" it with something (perhaps Spybot).  It is NTFS security wise Read Only, in which case Attrib won't work - nor will my script unless the user is an admin.
    LVL 10

    Expert Comment

    You are correct for Win XP, perhaps for Windows 2000 too.
    Not for NT4 and maybe Windows 98.
    This is not an XP or 2000 thread and Ben1211 don't talk us about wich OS he uses.
    Then independently of his OS i gave a solution wich works on all Windows platforms.
    LVL 95

    Expert Comment

    by:Lee W, MVP
    How can your script work for all OSs when the attrib command would do nothing on XP and 2000?  (I'd have to check NT4) and the path to the hosts file is different in 98.
    LVL 10

    Expert Comment

    What's your aim Leew ?
    I am not asking help at all on this.
    Try my .bat
    It works an all NT Systems.
    And even your hosts file is read-only or not, it does nothing but no errors are displayed.
    LVL 95

    Expert Comment

    by:Lee W, MVP
    My AIM is to help the user and not mislead him.

    You said:
    > Then independently of his OS i gave a solution wich works on all Windows platforms.
    This is blatently not true.  Your solution will NOT work on Win9x or Me.  It can't because th host file will not be there.  Mine won't work for these systems either, but I'm not saying it will.

    > attrib -R %windir%\system32\drivers\etc\hosts
    Again, the Attribute is NOT set and the file permission IS set. Explain how this can change NTFS permissions?  In my experience and understanding, it simply cannot.  Thus, for normal user accounts and at least in 2000 and XP, this is pointless and accomplishes nothing.

    You also said:
    > I am not asking help at all on this.
    I'm trying to help the requestor.  If you are absolutely certain, then TEACH ME SOMETHING.  I'm open to learn, but I require intelligent debate and evidence before I will take the word of someone who I know little if anything about (you have nothing in your profile so I don't know your possible skill level).

    > Try my .bat
    > It works an all NT Systems.
    > And even your hosts file is read-only or not, it does nothing but no errors are displayed.

    It may work on all NT *4* systems (I have my doubts but since I don't have an NT 4 system handy, I'm not arguing that).  BUT, it's a fairly safe bet he is not using NT4.  Aside from the fact it is 9 years old, the copy of a hosts file he posted has a copyright date of 1999, and, excluding his apparent additions, the text he posted is the same as included with Windows 2000 and XP.
    LVL 10

    Expert Comment

    Hi Leew,
    I posted a solution wich "works an all NT Systems."
    Please forget the "all Windows platforms."
    Most of the time i do not answer questions about Win9x.
    I do not have the time.
    I have 15 years of experience. I began in February 1990.
    I am not at the right place to dabate.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Introduction How to create multiboot configuration with XP\Vista and Windows 7 on it? And most important question - how to do this correctly so not to have any kind of nightmares we get when system gets screwed? First of all one should realize t…
    #Citrix #POC #XenDesktop #vCenter #VMware #ESX
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now