How to Set a Static IP Address in Batch Script
While most client computers use DHCP to get an IP address automatically, servers and network devices often require a static IP address, i.e. a permanent, unchanging address that ensures they are always reachable at the same location. Automating this configuration is a common task for server deployments, network setups, or for creating a "rescue" script to restore a known-good network configuration. The standard command-line tool for this is the powerful netsh (Network Shell).
This guide will teach you how to use netsh from a batch script to set a static IP address, subnet mask, default gateway, and DNS servers. You will learn the critical prerequisites for this operation, including the absolute need for administrator rights and the exact network information required.
CRITICAL: Prerequisites and Information Needed
Before you run any script to set a static IP, you must have the following information:
- The Exact Adapter Name: You must know the name of the network interface you want to configure (e.g.,
"Ethernet","Local Area Connection"). You can find this by runningnetsh interface show interface. - The Static IP Address: The IP you want to assign (e.g.,
192.168.1.100). - The Subnet Mask: The subnet for your network (e.g.,
255.255.255.0). - The Default Gateway: The IP address of your router (e.g.,
192.168.1.1). - The DNS Server(s): The IP address(es) of your DNS servers (e.g.,
8.8.8.8).
This operation must be run with administrator privileges.
The Core Commands: netsh interface ipv4
The netsh utility's interface ipv4 context is where all the configuration happens. This is a two-part process: first you set the IP address, then you set the DNS servers.