Programmatically Changing Your IP Address

A friend needs to change back and forth between a DHCP network and a static IP network. Rather than teach her how to go in and configure the settings manually, I wanted to write her a program that would automate everything.

I found the registry settings that control everything, but after changing them, you need to reboot. There had to be a better way.

I finally stumbled across netsh and it's a lot more powerful than I knew. It turns out, my problem can be solved with a batch file. To set the network interface to use DHCP, run this:

netsh interface ip set address name="Local Area Connection" dhcp
netsh interface ip set dns name="Local Area Connection" dhcp

To flip over to a static IP configuration, use something like this:

netsh interface ip set address name="Local Area Connection" static 192.168.1.12 255.255.255.0 192.168.1.1 1
netsh interface ip set dns name="Loca Area Connection" static 192.168.1.1

The order of address in that command is IPAddress, Subnet Mask, Local Gateway. The address in the second command is the DNS server.