Peter Hinchley

Configuring the IP Address of a Network Adapter from the Command Line

Tagged: cmd, network

I have disabled DHCP on my home wireless router and use static addresses on all internet connected devices. There are numerous benefits to operating this way, but a downside is the need to frequently switch between the static IP configuration used at home, and the dynamic configuration typically required when out in the field.

I've simplified the process of switching between these configurations by creating two small scripts. The first script configures the wireless network connection of my computer for DHCP.

netsh interface ip set address "wireless network connection" dhcp
netsh interface ip set dns "wireless network connection" dhcp

The second script restores the static configuration used at home. The first line sets an IP address of 192.168.0.2, a mask of 255.255.255.0, and a default gateway of 192.168.0.1. The second line of the script sets a primary DNS server address of 192.168.0.1.

Note: The caret symbol (^) is used to split each command across two lines.

netsh interface ip set address "wireless network connection" ^
static 192.168.0.2 255.255.255.0 192.168.0.1
netsh interface ip set dns "wireless network connection" ^
static 192.168.0.1 primary

The scripts are written to modify the properties of a connection named wireless network connection, but can be easily changed to work with others, such as local area connection.

I've saved the code into batch files called dhcp.cmd and static.cmd, and added shortcuts to the files from the Start Menu. The shortcuts are configured to Run as administrator (an option available from the Advanced Properties dialog), as the commands must execute with administrative privileges.

Your Say