How To Change Network Interface Card (NIC) Settings Programmatically

by Stanley Fong


Why?

This comes about when I have to use my notebook outside my office where a hardwired LAN is available.
I have 2 work locations where 2 LAN’s are connected via a leased line.
Changing my NIC settings was easy because with Windows XP you don’t need to reboot and in my case I only
have to change one digit at the IP address. This job could be tedious at my client’s office with
a completely different LAN setup.

A NIC can only have one set of settings; if you can afford few NIC’s for your notebook then you can skip this article.

How?

Being lazy I didn’t want to enter IP address, gateway, WINS, etc. every time I changed location, therefore I
scoured the Internet for a solution. I love the Internet. I usually find what I want and learn something
along the way.

Methods

A. MCSEworld by Daniel Petri

Some little tricks are available at MCSEworld by Daniel Petri, see http://www.petri.co.il.
The basic trick is to use netsh.exe to dump your current setting to a text or a data file.
This can be done by typing below:

Netsh interface dump > text.txt

The netsh scripting tool allows you to import the setting by "executing" a text or data file.
To do the reverse, just type below

Netsh exec text.txt

The trouble of course is all these have to be done manually at command prompt within a DOS window.
At first I did try this, to my surprise it didn’t work and all I got is error message like below

'register' is not a valid argument for this command.
The syntax supplied for this command is not valid. Check help for the correct syntax.

Usage: add dns [name=]<string. [addr=]<IP address> [[index=]<integer]

Parameters:.........
.
.
.
.

Dhcp is already enabled on this interface

I have not copied everything I saw but you’ll see it’s not that simple after all.
The point to note the last line "Dhcp is already enabled on this interface",
I’ll explain this later in the article.

Netsh.exe is a command line scripting tool available on Windows 2000, XP and Windows server 2003.
If you type netsh in Windows Help you’ll get to know something basic about it.

B. The Code Project – SwitchNetConfig by Omar Al Zabir

This is a good one, see it at http://www.codeproject.com/search.aspx?q=SwitchNetConfig&sbo=kw

This little utility is written in C# using functionalities available from WMI (Windows Management Instrumentation),
again another powerful scripting tool for network administrators.

I tried it, didn’t like it 100% because I can’t tweak the code to suit my taste.
I don’t use C# to earn a living just as simple as that.
It surely should work fine.

C. IPetC

This is a little utility which is good for simple settings.
You’ll have to pay for it if you like it after trial for 28 days,
see http://www.ipetc.com

It looks like this:

IPetC - Screen Capture
Figure 1: IPetC – Screen Capture

IPetC - Profile Configuration
Figure 2: IPetC – Profile Configuration

As you can see from screen shots above you could only set simple things only.
This is handy enough for normal users.

D. BATCH file

I found the batch file solution in http://www.experts-exchange.com

Someone asked a question similar to mine and expert "sirbounty" offered two solutions.
The first is to use batch file to execute the required netsh commend with parameter fed to it.
The second is to use VBScript to run from a command prompt via cscript. Again for the second
solution WMI objects will be called.

I liked the first solution. You can either have a simple batch file containing below

@netsh int ip set addr local static 62.131.44.88 255.0.0.0 62.1.1.1

or a batch file containing details of few locations you normally go:

-------ConfigIP.bat----------------------------
@echo off
::Run with
if [%1]==[] goto error
goto %1
:work
netsh int ip set addr local static 62.131.44.88 255.0.0.0 62.1.1.1
goto :eof
:school
netsh int ip set addr local static 10.0.0.26 255.0.0.0 10.0.0.1
goto :eof
:home
netsh int ip set addr local static 192.168.1.104 255.255.255.0 192.168.1.1
goto :eof
:error
cls
Echo. Valid parameters are Work, School, Home
Echo. Example: ConfigIP Work

My Solution

I eventually decided on a combination of a few of the methods above. I want a batch file to be able to achieve:

  • Interactive with user, i.e. I don’t need to remember what parameter to be fed after the batch file. This will require user input after batch file is executed.
  • Read from text files with parameters. With this I only need to change details in text file to fine tune NIC setting.
  • Be able to tweak all settings of the NIC, i.e. not just IP, subnet mask and gateways.
  • Leave setting of wireless LAN alone as you don’t normally set it.

Below is my batch file

@echo off
title Contribute $10 for every use please - Stanley Fong August 2004
setlocal
set OK=N
:again
	set /p choice=Please enter 1[Kowloon Bay], 2[Tai Po], 3[Ebara] or END ??
	if /i [%choice%]==[END] endlocal&goto end
	if [%choice%]==[] goto again
	if [%choice%]==[1] goto 1
	if [%choice%]==[2] goto 2
	if [%choice%]==[3] goto 3
	set /p xxx=wrong entry, press any key to exit.
	endlocal
	goto end
:1
	echo NIC setting for Kowloon Bay being configured.....
	@netsh exec kb.txt
	set /p see=IP changed successfully for Kowloon Bay setting [9]see new setting [Enter]exit...
	if [%see%]==[9] goto show
	echo OK you don't want to see new setting, program ends
	goto end
:2
	echo NIC setting for Tai Po being configured.....
	@netsh exec tp.txt
	set /p see=IP changed successfully for Tai Po setting [9]see new setting [Enter]exit...
	if [%see%]==[9] goto show
	echo OK you don't want to see new setting, program ends
	goto end
:3
	echo NIC setting for Ebara Office being configured.....
	@netsh exec ebara.txt
	set /p see=IP changed successfully for Ebara office setting [9]see new setting [Enter]exit...
	if [%see%]==[9] goto show
	echo OK you don't want to see new setting, program ends
	goto end
:show
@netsh int ip show config
:end

With the "/p" switch of the "SET" command I can get user input of the location.
You don’t have to remember what to enter, just enter a number of your choice.
At the end when it’s successfully executed you could see your NIC setting.
This batch file will read text files containing NIC settings which must be in the same directory with the batch file.

Why Method "A" Above Didn’t Work

If you follow the example in (A) above to dump your NIC setting to a text file named
"text.txt" and edit "text.txt" with notepad you’ll see:

#========================
# Interface configuration
#========================
pushd interface

reset all

popd
# End of interface configuration

#========================
# Interface configuration
#========================
pushd interface ipv6

uninstall

popd
# End of interface configuration

# ----------------------------------
# ISATAP Configuration
# ----------------------------------
pushd interface ipv6 isatap

popd
# End of ISATAP configuration

# ----------------------------------
# 6to4 Configuration
# ----------------------------------
pushd interface ipv6 6to4

reset

popd
# End of 6to4 configuration

#========================
# Port Proxy configuration
#========================
pushd interface portproxy

reset

popd
# End of Port Proxy configuration

# ----------------------------------
# Interface IP Configuration
# ----------------------------------
pushd interface ip

# Interface IP Configuration for "Local Area Connection"

set address name="Local Area Connection" source=static addr=192.168.4.49 mask=255.255.255.0
set address name="Local Area Connection" gateway=192.168.4.1 gwmetric=0
add address name="Local Area Connection" gateway=192.168.32.1 gwmetric=0
set dns name="Local Area Connection" source=static addr=192.168.1.218 register=PRIMARY
set wins name="Local Area Connection" source=static addr=192.168.11.21
add wins name="Local Area Connection" addr=192.168.13.21

# Interface IP Configuration for "Wireless Network Connection"

set address name="Wireless Network Connection" source=dhcp
set address name="Wireless Network Connection" gateway=10.0.1.1 gwmetric=0
set dns name="Wireless Network Connection" source=dhcp register=PRIMARY
set wins name="Wireless Network Connection" source=dhcp

popd
# End of interface IP configuration

The above is your current NIC setting which includes something that you don’t normally require to set
such as ISATAP configuration and your wireless NIC. In fact this file contains a set of commands
which will configure your NIC using netsh.exe, i.e. when you do the reverse by "executing"

it using netsh.

What you need is a bare minimum text file like below (line number added)

1	# ----------------------------------
2	# Interface IP Configuration
3	# ----------------------------------
4	pushd interface ip


5	# Interface IP Configuration for "Local Area Connection"

6	set address name="Local Area Connection" source=static addr=192.168.32.49 mask=255.255.255.0
7	set address name="Local Area Connection" gateway=192.168.4.1 gwmetric=0
8	add address name="Local Area Connection" gateway=192.168.32.1 gwmetric=0
9	set dns name="Local Area Connection" source=static addr=202.77.0.2 register=primary
10	add dns name="Local Area Connection" addr=192.168.1.218 index=1
11	add dns name="Local Area Connection" addr=202.77.0.1 index=2
12	set wins name="Local Area Connection" source=static addr=192.168.11.21
13	add wins name="Local Area Connection" addr=192.168.13.21

14	# Interface IP Configuration for "Wireless Network Connection"

15	# normally wireless LAN is not required to be set again

16	# set address name="Wireless Network Connection" source=dhcp
17	# set address name="Wireless Network Connection" gateway=10.0.1.1 gwmetric=0
18	# set dns name="Wireless Network Connection" source=dhcp register=primary
19	# set wins name="Wireless Network Connection" source=dhcp


20	popd
21	# End of interface IP configuration

Note "#" has remarked command lines which will be ignored by netsh but will help
you next time when you want to put them back. This will get rid of the message "Dhcp
is already enabled on this interface". This will also speed up execution of the text file.

The funny thing is that "executing" the text file above (bare minimum) will get exactly
the same setting at your NIC as in the long listing of the dumped "text.txt" above.
Only Microsoft can explain why this happened. After few trials I just know how to put it right.
The critical part is at line 9, 10 and 11 above.

9	set dns name="Local Area Connection" source=static addr=202.77.0.2 register=primary
10	add dns name="Local Area Connection" addr=192.168.1.218 index=1
11	add dns name="Local Area Connection" addr=202.77.0.1 index=2

From the "text.txt" listing, it has one line only

set dns name="Local Area Connection" source=static addr=192.168.1.218 register=PRIMARY

If you look up your NIC settings the normal way you’ll see

TCP/IP - WINS
Figure 3: TCP/IP – General

TCP/IP - DNS
Figure 4: TCP/IP – DNS

TCP/IP - Advanced
Figure 5: TCP/IP – WINS

 TCP/IP - General
Figure 6: TCP/IP – Advanced

To do the setting right and if the DNS server addresses are really critical in your case you
have to play around with the "register=primary" and "index=?" in the
"SET" and "ADD" command lines respectively. You’ll have to do few trials
to get it right. Note you can’t use "register=primary" in the "ADD" command.
Microsoft has acknowledged this as a problem, see

http://support.microsoft.com/default.aspx?scid=kb;en-us;818835
This is why Method A above didn’t work.

Dumping Your Settings To Text Files

When finding out why text file dumped didn’t work, it was discovered that the
"dump" command is available at different context in the netsh scripting
tool therefore you must be careful when using the dumped text file to reload your settings.
You can use "dump" within the following context of netsh

Netsh interface ip
Netsh interface ipv6
Netsh interface portproxy
Netsh ras ip
Netsh ras ipx
Netsh ras aaaa
Netsh ras appletalk
Netsh ras appletalk
Netsh routing ip
Netsh routing ipx
Netsh routing ipx netbios
Netsh routing ipx rip
Netsh routing ipx sap
Netsh bridge
Netsh diag
Netsh firewall

Give it a try you’ll see the deeper you go in the context the more specific setting you’ll see
at the console by running "dump" at the prompt. Therefore it’s safer to use bare
minimum settings in a text file if you don’t really want to mess around with other settings.
I have tried using "exec" to reload text files dumped from the context of
"netsh", "netsh interface" and "netsh interface ip" all
resulted in error caused by the "register" switch.

It’s fun after all, for more information about netsh visit Microsoft sites at:

More by Author

Must Read