Skip to main content

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:

  1. 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 running netsh interface show interface.
  2. The Static IP Address: The IP you want to assign (e.g., 192.168.1.100).
  3. The Subnet Mask: The subnet for your network (e.g., 255.255.255.0).
  4. The Default Gateway: The IP address of your router (e.g., 192.168.1.1).
  5. 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.

Step 1: Setting the Static IP Address, Subnet, and Gateway

The first command configures the core IP information.

Syntax: netsh interface ipv4 set address name="<AdapterName>" static <IP_Address> <SubnetMask> <Gateway>

  • name="<AdapterName>": The exact name of your network connection.
  • static: Specifies that we are setting a static IP.
  • The three arguments that follow are the IP, subnet mask, and gateway.

Example:

netsh interface ipv4 set address name="Ethernet" static 192.168.1.100 255.255.255.0 192.168.1.1

Step 2: Setting the DNS Server Addresses

Setting the DNS is a separate but equally important command.

Syntax: netsh interface ipv4 set dnsserver name="<AdapterName>" static <PrimaryDNS> primary

To add a secondary DNS server, you run a second command: netsh interface ipv4 add dnsserver name="<AdapterName>" <SecondaryDNS> index=2

Example:

REM Set the primary DNS server
netsh interface ipv4 set dnsserver name="Ethernet" static 8.8.8.8 primary

REM Add a secondary DNS server
netsh interface ipv4 add dnsserver name="Ethernet" 8.8.4.4 index=2

How to Revert Back to DHCP (Automatic IP)

Just as important as setting a static IP is knowing how to undo it. You can switch an adapter back to DHCP with two simple commands.

@ECHO OFF
REM This script reverts the "Ethernet" adapter back to DHCP.
REM Run as Administrator.

ECHO Step 1: Reverting IP address configuration to DHCP...
netsh interface ipv4 set address name="Ethernet" dhcp

ECHO Step 2: Reverting DNS configuration to DHCP...
netsh interface ipv4 set dnsserver name="Ethernet" dhcp

ECHO --- Configuration reverted to Automatic (DHCP) ---

Common Pitfalls and How to Solve Them

Problem: The Script is Not Run as Administrator

This is the number one reason for failure.

Example of error message:

The requested operation requires elevation.

Solution: You must run the script from an elevated command prompt. Right-click your .bat file or cmd.exe and select "Run as administrator."

Problem: Using the Wrong Network Adapter Name

If the name in the name="..." part is incorrect, the command will fail.

Example of error message:

An interface with this name is not registered with the router.

Solution: Before running the script, always verify the exact adapter name by running netsh interface show interface.

Problem: The IP Address is Already in Use

If you try to assign a static IP that another device on the network is already using, Windows will detect the conflict and the command may fail or your network connection will be unstable.

Solution: Ensure the static IP address you plan to use is outside the range of addresses automatically assigned by your DHCP server and is not currently in use by another device.

Practical Example: A Full Static IP Configuration Script

This script provides a complete template for setting a static IP. It defines all the necessary variables at the top for easy configuration.

@ECHO OFF
SETLOCAL
TITLE Static IP Configuration Utility
REM This script must be run as an Administrator.

REM --- CONFIGURATION ---
SET "AdapterName=Ethernet"
SET "StaticIP=192.168.1.150"
SET "SubnetMask=255.255.255.0"
SET "Gateway=192.168.1.1"
SET "PrimaryDNS=8.8.8.8"
SET "SecondaryDNS=8.8.4.4"

ECHO --- Applying Static Network Configuration ---
ECHO This will configure the '%AdapterName%' adapter.
ECHO.
PAUSE

ECHO Step 1: Setting static IP address...
netsh interface ipv4 set address name="%AdapterName%" static %StaticIP% %SubnetMask% %Gateway%

ECHO.
ECHO Step 2: Setting DNS servers...
netsh interface ipv4 set dnsserver name="%AdapterName%" static %PrimaryDNS% primary
netsh interface ipv4 add dnsserver name="%AdapterName%" %SecondaryDNS% index=2

ECHO.
ECHO --- Static IP configuration has been applied ---
ECHO Verifying new configuration...
ipconfig | findstr "IPv4 Address"

PAUSE
ENDLOCAL

Conclusion

The netsh command is the definitive tool for automating network configuration in Windows. By using its interface ipv4 context, you can reliably set a static IP address, gateway, and DNS servers from a batch script.

Key takeaways for success:

  • You must run the script as an Administrator.
  • You must know the exact adapter name and the correct IP configuration details.
  • Setting the IP address (set address) and the DNS servers (set dnsserver) are two separate commands.
  • Know how to revert to DHCP to undo your changes.