Partilhar via


Automate Network Adapter Configuration using NETSH

So I'm kind of lazy.  Not in the "sleep late and don't go to work" slacker way, I just don't like doing dull repetitive tasks if they can be automated in some way. As a rule if I have to do the same task more than a couple of times I'm writing a script (or getting someone else to do it).

During the years I worked as a consultant it was not uncommon to connect my laptop to several different networks in the same day. In some cases they were DHCP enabled so connection was easy. For others I would have to configure the network adapter manually. Ugh!

Enter the NETSH commands.  The NETSH command can be used to modify the network configuration on Windows 2000 and later computers. It's not the friendliest syntax to use but it is a real time saver once you learn to use it (I have noticed the version that ships with Vista seems almost intuitive). Listed below are sample script for using NETSH to set STATIC IP entries on an adapter and another script to set the adapter back to DHCP mode so the settings can be obtained automatically. The command syntax for Vista and XP are slight different due to the fact that Vista supports IPV6 natively so sample scripts for each OS are listed below. To use the code, paste it into a batch file and modify the "name=" to the name of the adapter in quotes and change the IP addresses.

VISTA - Static IP

netsh interface ipv4 set address name="Wireless Network Connection 2" source=static addr=192.168.0.100 mask=255.255.255.0 gateway=192.168.0.250 gwmetric=0
netsh interface ipv4 set dnsserver name="Wireless Network Connection 2" source=static addr=192.168.0.2 register=NONE
REM netsh interface ipv4 set wins name="Wireless Network Connection 2" source=static addr=155.217.27.9
REM OR if no WINS server
netsh interface ipv4 set winsserver name="Wireless Network Connection 2" source=dhcp
ipconfig /all

VISTA - DHCP

netsh interface ipv4 set address name="Wireless Network Connection 2" source=dhcp
netsh interface ipv4 set dnsserver name="Wireless Network Connection 2" source=dhcp
netsh interface ipv4 set winsserver name="Wireless Network Connection 2" source=dhcp
ipconfig /renew "Wireless Network Connection 2"
ipconfig /all

XP - Static IP

netsh interface ip set address name="BROADCOM" source=static addr=192.168.27.40 mask=255.255.255.224
netsh interface ip set address name="BROADCOM" gateway=192.168.27.33 gwmetric=0
netsh interface ip set dns name="BROADCOM" source=static addr=192.168.98.12 register=NONE
netsh interface ip set wins name="BROADCOM" source=static addr=none

XP - DHCP

netsh interface ip set address name="BROADCOM" dhcp
netsh interface ip set dns name="BROADCOM source = dhcp
netsh interface ip set wins name="BROADCOM" source=dhcp

Comments

  • Anonymous
    October 27, 2006
    Hi! I need to get the name of the TCP/IP Interface in Windows XP (for example: "LAN"). Wich command can I use to do that? Thanks for you help :)

  • Anonymous
    October 28, 2006
    To see the names of your network adapters open a command prompt, type IPCONFIG /ALL and press enter.  Use the name shown in your NETSH scripts.  If the adapter name has spaces enclose name in quotes (" ").

  • Anonymous
    May 24, 2007
    Is there a way to query the exact name of the TCP/IP interface?  I'd like to use netsh in scripts and I would like to be able to set a variable and then feed the variable to script.  As not all computers have the same network interface name, I'd like to query it first.  Even more helpful would be a way to identify which one currently maps a specific IP address (as there could be multiple NICS, some of which are disabled).

  • Anonymous
    July 28, 2007
    To know the interface names use the command   netsh interface ip show address I am trying to devise a way to automate the passage of the name to the script (maybe without power shell) Let's see...

  • Anonymous
    August 17, 2007
    i want to make that script for vista without putting the name from connection is that possible?

  • Anonymous
    September 07, 2007
    In order to modify the properties of an adapter you need to reference the adapter name in the command.  I don't know of any other way to accomplish this.  If writing a script, you use WMI code to loop through the adapters collection looking for an adapter with specific propeties, then capture the name and pass that to the NETSH commands.  Not easy but possible.  YOu csn check out the Microsoft Scripting site here <http://www.microsoft.com/technet/scriptcenter/default.mspx?wt.svl=leftnav > for examples and resources.

  • Anonymous
    October 29, 2007
    Thanks for this fantastic script! Going to work changing the IP, coming back home, back to DHCP was a real pain. Thanks again!

  • Anonymous
    December 14, 2007
    Can you use this command to automate Wireless config and WEP keys?

  • Anonymous
    January 09, 2009
    Excellent article! When I install a second virtual adapter it comes up as "Local Area Connection 2". How can I rename it to "TAP_Connection" from e.g. a vbs script? This is on WinXP. Thanks in advance.

  • Anonymous
    May 10, 2009
    Many thanks for this tip..it's really handy. i need to swap everyday from dhcp to fixed ip and the other way around. Now the task is straight forward and fast!

  • Anonymous
    October 20, 2009
    than you very much. Nicely done.

  • Anonymous
    January 17, 2010
    When setting a static ip address on windows 7 using  netsh, the command returns but the ip address hasnt being changed yet and there is no output from the command. There is a delay of less than 15 seconds between the command returning and the IP address being changed. In Xp, the command only returns once the IP has been successfully changed over and "Ok" is outputted to standard output. Is there any way we can tell the netsh command on windows 7 to not return until the static IP address has been changed over? (i dont want to have to use the sleep command)