More and more I find myself needing to temporarily set static IPs on my laptop to access equipment for configuration or management, and changing IP addressing in Windows is easy enough, but I often either forget the settings I need or need to switch back and forth quickly and it becomes veryb time-consuming or tedious.
After searching around for a way to do this I found that scripting the IP changes via netsh in a batch file was a great way to not only change the IPs quickly and easily, but relieve myself from having to remember the IP addresses — I could simply save each configuration in a seperate batch file and name it appropriately. I could then switch back and forth quickly.
So here’s how to do it:
First, you’re going to need to know the basics of creating a batch file. If you’re not sure how to do it, click here to read how; it’s actually quick and easy.
Next, I’m going to show you some examples and the parameters, and then further below there will be a template batch file you can copy and easily edit.
Syntax of setting IP address:
netsh int ipv4 set address name=<interface name> source=<'static' | 'dhcp'> [address=<IP address[/bitmask]>] [mask=<netmask>] [gateway=<gateway>]
Syntax of setting DNS server address:
netsh int ipv4 set dns name=<interface name> source=<'static' | 'dhcp'> [address=<address | 'none'>] [register=<'none' | 'primary' | 'both'>] [validate=<'yes' | 'no'>]
Here’s a short run-down of what the various parameters mean. For further reading, consult the Microsoft Technet article on netsh.
Frequently used ‘address’ parameters are…
- name = Interface name or index.
(If you’re setting this up on a wired LAN connection, it’s usually “Local Area Connection” (not case sensitive), check control panel if you’re unsure.)
- source = ‘dhcp’ or ‘static’
- address = IP address, optionally the subnet prefix length192.168.1.1 or 192.168.1.1/24
- mask = The IP subnet mask for the specified IP address
(use subnet prefix length in address -or- mask, not both)
- gateway (optional) = ; or ‘none’ (default)
Frequently used ‘dns’ parameters….
- name = The name or index of the interface. (See note above)
- source = ‘dhcp’ or ‘static’
- address = IP address or ‘none’
- register = One of the following values:
- none: Disables Dynamic DNS registration.
- primary: Register under the primary DNS suffix only.
- both: Register under both the primary DNS suffix, as well as under the connection-specific suffix.
- validate = Specifies whether validation of the DNS server setting will be performed. The value is yes by default. (Passing ‘no’ is suggested to prevent scripting issues)
The following are a few examples of how to set addresses and DNS servers. Feel free to use them or change them however you like.
Example to set static IP, mask, and gateway address:
netsh int ipv4 set address name="Local Area Connection" source=static address=10.127.86.25 mask=255.255.255.240 gateway=10.127.86.30
An example to set static DNS (you may have to use this in conjunction with the above)
netsh int ipv4 set dns name="Local Area Connection" source=static address=10.127.70.1 register=primary validate=no
So if you needed, you could combine the above two commands into a single batch file, like so:
netsh int ipv4 set address name="Local Area Connection" source=static address=10.127.86.25 mask=255.255.255.240 gateway=10.127.86.30 netsh int ipv4 set dns name="Local Area Connection" source=static address=10.127.70.1 register=primary validate=no
Important: If you are using this on a Windows XP or another system without iPv6 support, change ‘ipv4′ to ‘ip’. Also, make sure your cable is unplugged before setting a static IP, else the command will freeze.
To set both address and DNS to DHCP:
netsh interface ipv4 set address name="Local Area Connection" source=dhcp netsh interface ipv4 set dnsservers name="Local Area Connection" source=dhcp
Important: There is a confusing disconnect between netsh setting DHCP and what is reported in the network connection properties. If you set a static IP, then later set DHCP, the static IP will still appear in the network connection properties dialog. However, once you plug into a DHCP-enabled network, your network adapter will configure properly. It doesn’t look like it will work, but it does.
If you want to make easy-to-use static IP scripts, create some batch files with helpful prompts like the following:
@echo Be sure NIC cable is unplugged and @pause netsh interface ipv4 set address name="Local Area Connection" source=static address=10.255.0.2 mask=255.255.255.252
An example of a user-friendly DHCP script
netsh interface ipv4 set address name="Local Area Connection" source=dhcp netsh interface ipv4 set dnsservers name="Local Area Connection" source=dhcp @echo It may take a few moments for changes to take effect. You may close this window or @pause
Important: In order for any batch file changes to work, you MUST right-click and ‘Run as administrator’ on Windows Vista and Windows 7.
If you have any comments or feedback from the above, please feel free to share in the comments section below. Thank you!