Related Items  

Linux and Windows IT Support  


Windows, Apple and Linux IT support and services.

For Software and Web Development.

Update hosts file with VBS (Windows)


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, "") <> 0 Then
End If
i = i + 1

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 & " intranet.corp www.intranet.corp intranet www.intranet")

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. 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.

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

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

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
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
9 Tuesday, 13 May 2014 09:00
It worked.
no permission
8 Saturday, 26 October 2013 13:18
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
6 Monday, 25 March 2013 13:46
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
if i have login as admin then also will it ask for permission or will it change directly???
4 Friday, 19 October 2012 15:38
Useful script..
modify IP instead of a hostname
3 Wednesday, 14 September 2011 09:08
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 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
Could you please tell me, if i want to modify the ip insead of hostname, so how do we do that?
1 Monday, 11 July 2011 12:07
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.

Add your comment

Your name:
  The word for verification. Lowercase letters only with no spaces.
Word verification:
yvComment v.1.24.0
Copyright © 1999 - 2020 Virtual Helpme | Business IT Support | Original Template: Allrounder