Remove Hosts file entries

Posted on 2009-04-24
Last Modified: 2012-05-06
I need a script to remove about 100 entries from an existing Hosts file from Windows XP PC's. The IP addresses are changing and DNS will now be provided via our Win2k3 server. I know I cannot use a wildcard in Hosts files, so what do you recommend.  We cannot replace the Hosts file because there are other entries that are not in DNS yet. Thanks for your help.
Question by:603currier
    LVL 6

    Expert Comment

    Create a host fiel with the entries you want and then replace the existing files using a loginscript? Depending on the amount of clients you have it might be worth setting up dns properly...
    LVL 18

    Expert Comment

    >> "We cannot replace the Hosts file because there are other entries that are not in DNS yet."

    Are these additional entries you need the same on all machines? If so I would say to do the following:

    Create a generic hosts file with all these entries and save it.

    Create a script (Logon or via GPO) to copy this file over the existing one.

    Why not just wait until you have all the entries in DNS and blow away the hosts files? Seems like you are going to have a lot of fun troubleshooting name resolution problems if you are relying on hosts and DNS... JMO

    Author Comment

    Thanks for the responses.  The entity forcing this change is an external one and waiting until DNS is available for all sites is not any option.
    LVL 28

    Accepted Solution

    There's a free tool REMFILE from this location:

    This simple tool removes lines that include matching strings from a file.  A single iteration would look like this:
        REMFILE "string to remove" <C:\Windows\System32\Drivers\etc\Hosts. >c:\Windows\System32\Drivers\etc\HostsNew.
        COPY c:\Windows\System32\Drivers\etc\HostsNew.  C:\Windows\System32\Drivers\etc\Hosts.

    Or, you could do them in pairs:
        REMFILE "string to remove" <C:\Windows\System32\Drivers\etc\Hosts. >c:\Windows\System32\Drivers\etc\HostsNew.
        REMFILE "second string to remove" <C:\Windows\System32\Drivers\etc\HostsNew. >c:\Windows\System32\Drivers\etc\Hosts.

    You can then build a batch file with 100 lines in it to remove all the excess gunk from your HOSTS files.  

    Honestly, though, I echo the sentiments of the other folks -- leave the hosts file for now and blow it away when you are done.  This works as long as they are not changing the IP addresses at the same time.

    Author Comment

    They are changing the IP address unfortunately.  Thanks you've given me good ifno.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
    I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
    This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
    In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …

    760 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

    15 Experts available now in Live!

    Get 1:1 Help Now