• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 835
  • Last Modified:

How to write a CMD or VBS script to change the NIC settings on a group of PCs to DHCP

Hello experts,
I have a .txt file containing about 100pc hostnames. Unfortunatly we do not have AD set up where the PC's are in OU's. It would take as much time to manually add them to OU's as it would to use the netsh cmd file and push the settings.

I am wondering if somebody could help me write a script to read from a .txt file for the hostnames and then change the NIC settings to DHCP enabled?

Thanks much, i did post a question like this before, but i have not figured out how to pass the data from the text file to the script.

Thanks everybody : - )

I was thinking of this???(not sure, still new to VBScript)
Set Net = Wscript.CreateObject("Wscript.Network")
strComputer = ##read from txt file##

  • 2
1 Solution
This is a variation of my script I used to move my network workstations from a Static to a Dynamic IP Address scheme.  I used computer groups but it could easily be done with your text script upon login/logoff.  I haven't tried it with the netsh util.

<><>Begin Script<><>

On Error Resume Next

Set Net = Wscript.CreateObject("Wscript.Network")
Set FSO = CreateObject("Scripting.FileSystemObject")
strComputer = net.Computername
Set ListFile = FSO.OpenTextFile("textfile")
CompList = ListFile.readall

if inStr(CompList, strComputer) then

      Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
      strQuery = "Select * from Win32_NetworkAdapterConfiguration " & "where IPEnabled=TRUE"

      Set colNetAdapters = objWMIService.ExecQuery(strQuery)
      For Each objNetAdapter In colNetAdapters
            errEnable = objNetAdapter.EnableDHCP()
            errDNS = objNetAdapter.SetDNSServerSearchOrder(null)
            errWINS = objNetAdapter.SetWINSServer("", "")
            RemoveGTW strComputer, objNetAdapter.SettingID
end if

Sub RemoveGTW(strComputer, lsnicsid)
      Const HKEY_LOCAL_MACHINE = &H80000002
      arrStringValues = Array()
      strEntryName = "DefaultGateway"

      lsStrWMI = "winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv"
      Set objReg=GetObject(lsStrWMI)

      strKeyPath = "SYSTEM\CurrentControlSet\Services\" & lsNicSid & "\Parameters\Tcpip\"
      objReg.SetMultiStringValue HKEY_LOCAL_MACHINE,strKeyPath, strEntryName,arrStringValues

      strKeyPath = "SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\" & lsNicSid
      objReg.SetMultiStringValue HKEY_LOCAL_MACHINE,strKeyPath, strEntryName,arrStringValues

End Sub

<><>End Script<><>
You might want to change two of the lines to ensure that the script runs as anticipated:

strComputer = net.Computername -> strComputer = ucase(Net.ComputerName)

add this

CompList = ucase(CompList)  after CompList = ListFile.readall

I am not sure if you can do this instead CompList = ucase(ListFile.readall).
Go sneek a peek at this URL  :


Basically shows you how to programitcally change network card settings :)

Kind regards

sqlagent007Author Commented:
SR75, can you tell me where in your script i would enter the computer hostname? Assuming i don't get it from a file.

what if i just want to run this script 1 time on 1 PC? Where would the hostname go?


Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now