Windows: Script to Change Interface to Static or DHCP

I build out customer equipment in our lab near daily, typically with my laptop and I will have the wired into whatever I’m working on and wireless on our internal net to download firmware’s or copy configs etc.

I’ve always wanted to be able to set a static IP (with or without a gateway) without going to Control Panel > Network > Network Connections > Right Click > Properties > Scroll Down > Double Click > OK > OK etc. etc.

I found this batch file after doing a little searching, it does 90% of what I wanted to achieve.

https://community.spiceworks.com/how_to/320-batch-file-script-to-change-ip-addresses

Below is my adjusted version to include an option to add static with no GW.

The important part to note is this bit:

netsh int ip set address name = "Ethernet"

The address name = must match the interface name in Control Panel that you want to edit with this script. There are 3 places to change it in here if you need to.

@echo off
echo Choose:
echo [a] Set DHCP
echo [b] Set Static IP
echo [c] Set Static IP no GW
echo.
:choice
SET /P C=[a,b,c]?
for %%? in (a) do if /I "%C%"=="%%?" goto a
for %%? in (b) do if /I "%C%"=="%%?" goto b
for %%? in (c) do if /I "%C%"=="%%?" goto c
goto choice
:a
@ECHO OFF
ECHO Resetting IP Address and Subnet Mask to DHCP
netsh int ip set address name = "Ethernet" source = dhcp

ipconfig /renew

ECHO Here are the new settings for %computername%:
netsh int ip show config

pause
goto end

:b
@echo off
echo "Please enter Static IP Address Information"
echo "Static IP Address:"
set /p IP_Addr=

echo "Subnet Mask:"
set /p Sub_Mask=

echo "Default Gateway:"
set /p D_Gate=

echo "Setting Static IP Information"
netsh interface ip set address "Ethernet" static %IP_Addr% %Sub_Mask% %D_Gate% 1
netsh int ip show config
pause
goto end

:c
@echo off
echo "Please enter Static IP Address Information"
echo "Static IP Address:"
set /p IP_Addr=

echo "Subnet Mask:"
set /p Sub_Mask=

echo "Setting Static IP Information"
netsh interface ip set address "Ethernet" static %IP_Addr% %Sub_Mask%
netsh int ip show config
pause
goto end

:end

Leave a comment