Update hosts file with VBS (Windows)

Print

VBSccript_file_format_iconQ. I have some hosts that for some reason don't work with the DNS entries. Can I update hosts files on all machines automatically?

A. Ideally, you should fix the DNS, however, this is a quickfix and a show of VBS power in MS world. You can use the following script to achieve this.

This has been tested with Windows XP and Windows 7. You need Administrator rights for this to work. As a note, there's a part of the script that checks to see if the hosts file is read-only. If it is, it makes it writeable.

Const ForReading = 1, ForWriting = 2, ForAppending = 8, ReadOnly = 1
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell=CreateObject("WScript.Shell")
WinDir =WshShell.ExpandEnvironmentStrings("%WinDir%")

HostsFile = WinDir & "\System32\Drivers\etc\Hosts"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(HostsFile, ForReading)

Do Until objFile.AtEndOfStream
If InStr (objFile.ReadLine, "10.10.10.105") <> 0 Then
WScript.Quit
End If
i = i + 1
Loop
objFile.Close

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(HostsFile)
If objFile.Attributes AND ReadOnly Then
objFile.Attributes = objFile.Attributes XOR ReadOnly
End If

Set filetxt = fso.OpenTextFile(HostsFile, ForAppending, True)
filetxt.WriteLine(vbNewLine & "10.10.10.105 intranet.corp www.intranet.corp intranet www.intranet")
filetxt.Close
WScript.quit

What does the script do?

Using VBS, it does the following:

  1. Sets up the variables and specifies where the host file is
  2. Opens the host file for reading, looks to see if there is an IP entry in it already (ie. 10.10.10.105). We don't want it to append a new entry every time it runs. If it finds the entry, the script exits
  3. Next, lets see if the file is read only. If it is mark is as read/write (once again, need to be an administrator to do this)
  4. Finally, open the file and append the required host info at the bottom
  5. Close and exit

Feel free to modify or use it.

VB Script
Comments (12)
www.dedicatedhosting4u.com
12 Friday, 19 June 2020 07:37
Anant
Thanks for making a good point . You always show us a new way.

Thanks
DedicatedHosting4u.com
Hotlink .TXT file from specific domain in Host file
11 Tuesday, 25 April 2017 06:27
Edwin
I'm trying to hotlink the following URL in my Hosts file:

https://winhelp2002.mvps.org/hosts.txt

Purpose is to block all domains listed on TXT file on localhost.

Anyone has an idea?
Want to run it in user account
10 Thursday, 10 September 2015 04:03
Viraj
I want to run this script using normal user accounts startup login using GPO. but due to no permissions its not updating, can some one help me to add admin credentials in this script to run as admin within this vbscript only
THanks
9 Tuesday, 13 May 2014 09:00
jepoxerized
It worked.
no permission
8 Saturday, 26 October 2013 13:18
Daan
I am administrator and is still says I have no permision but I am not the only one that is a admin
can it be that

I tried this my self the only problem was that it has no extension like .txt I need that to work

I wanted to block fb on a friends PC and after a minete unblock it :p
install application
7 Saturday, 24 August 2013 06:08
Essam Sayed
i want install some programs by editing list from windows script host
thanking
6 Monday, 25 March 2013 13:46
vic
wow u r great bro good script....plz provide more scripts....thanks a lot again
host file permission
5 Saturday, 12 January 2013 07:48
archit
if i have login as admin then also will it ask for permission or will it change directly???
thanks
4 Friday, 19 October 2012 15:38
Prasad
Useful script..
modify IP instead of a hostname
3 Wednesday, 14 September 2011 09:08
Ant
@BalReddy
I'm not sure what do you mean by modifying the IP instead of a hostname.

If you are doing a search by hostname, just replace it in the script (eg: replace 10.10.10.105 with whatever your hostname is)

This particular script is adding a new host to the list, if it doesn't exist. I'll add another script (later) that will replace a string.
Nice one
2 Wednesday, 07 September 2011 23:43
BalReddy
Could you please tell me, if i want to modify the ip insead of hostname, so how do we do that?
nice
1 Monday, 11 July 2011 12:07
Tom
Good example - thanks! I've had an issue with Spybot S&D adding a bunch of URLs to the hosts file. Was too lazy to clean it up manually.
yvComment v.1.24.0