Using a BAT File to Set IP Address

I know a feel people who have had a lot of problems moving their laptops from one network to another. This is mostly due to one of the networks requiring a static IP address, and not using DHCP. This means they have to manually change from static back to DHCP as they change networks. Doing a little research I was able to come up with two bat files, one to set the static IP for them, and one to set it back to DHCP when they are done. Let’s take a look at the two bat files.

First we will take a look at the bat file that sets the network adapter to a static IP. The code for this is below:

@echo off

set varip=10.10.10.10
set varsm=255.255.255.0
set vargw=10.10.10.1
set vardns1=8.8.8.8
set vardns2=8.8.4.4

echo Setting Local Area Connection to Static IP

::Setting IP Information
netsh interface ipv4 set address name=”Local Area Connection” source=static addr=%varip% mask=%varsm% gateway=%vargw%

::Setting DNS
netsh interface ipv4 set dnsservers name=”Local Area Connection” source=static address=%vardns1% validate=no
netsh interface ipv4 add dnsservers name=”Local Area Connection” addr=%vardns2% index=2 validate=no

I set variables so we can set all the values for the IP information, that way it makes the bat file a lot easier to change and update with different IP information. Looking at the next part where we set the IP information, you can see we specified Local Area Connection. If you wanted to change this to work on your wireless connection you can simply change this to say Wireless Network Connection. The code then sets all the network information such as the IP address, subnet mask, and gateway. In the next section I set the DNS values.  The first line of code changes the DNS to static and sets the first DNS value to the first DNS variable.  The second line of code then adds in the second DNS variable value.  After that you are all set and your network connection should be set to the static IP address specified in the bat file.

But let’s say you need to switch your network connection back to using DHCP, that is actually a lot easier to do than setting a static.  The bat file I wrote for this is as follows:

@echo off

echo Setting Local Area Connection to DHCP

netsh interface ipv4 set address name=”Local Area Connection” source=dhcp
netsh interface ipv4 set dnsservers name=”Local Area Connection” source=dhcp
netsh interface ipv4 set winsservers name=”Local Area Connection” source=dhcp

Basically this simple goes through the IP address, DNS, and WINS and makes sure to set them all back to DHCP.  This one is fairly simple, but effective.  After running this your adapter will be back to using DHCP.  Like I stated earlier if you wanted to use this for your wireless you can simple change Local Area Connection to Wireless Network Connection.

So if you find yourself having to switch between a static network and a DHCP network often, you may find these two little bat files of use.  If nothing else, they will make having to know all your network information for the static network easier as switching over will be as simple as double click on a file.