Change IP Address from the Command Prompt

Changing the IP settings from the Windows GUI is a no-brainer! I have never been a fan of using the GUI for system configuration. I change the network settings of the ethernet and wifi interfaces from the command line!

There is a Windows utility called netsh, that will allow us to change system networking settings. As you will need to modify the system settings, you will need to run the netsh with Administrator privileges.

To set a static IP address, subnet mask and the default gateway for your ethernet interface, run the following command:

C:\>netsh interface ip set address name="Local Area Connection" source=static address=192.168.1.10 mask=255.255.255.0 gateway=192.168.1.1

Note that the default gateway address is optional and you have to specify the network interface either by it’s name or by the index. If you prefer to specify the network interface with the index number, you can retrieve the indexes from running the following command:

C:\>netsh interface ip  show interface

You are also to modify the DNS settings similarly:

C:\>netsh interface ip set dns "Local Area Connection" static 192.168.1.1

Below are two batch files that I frequently use to modify the IP address settings.

Copy the following code to a file called staticip.bat and keep it somewhere in your PATH.

@echo off
setlocal enableextensions enabledelayedexpansion

set argc=0
for %%x in (%*) do set /A argc+=1

if %argc% LEQ 2 goto help
if %argc% == 3 goto nogw
if %argc% == 4 goto dogw

goto help

:dogw
netsh interface ip set address name="%1" source=static address=%2 mask=%3 gateway=%4
goto endp

:nogw
netsh interface ip set address name="%1" source=static address=%2 mask=%3
goto endp

:help
echo.
echo Usage: staticip interface-name ipaddress netmask [gateway] 
echo.
echo Assigns a static IP to the network interface. 
echo If the gateway is not defined, a default route is not created  
echo. 

:endp

Copy the following code to a file called dhcpip.bat and keep it somewhere in your PATH.

@echo off
setlocal enableextensions enabledelayedexpansion

set argc=0
for %%x in (%*) do set /A argc+=1

if %argc% == 0 goto help
if %argc% == 1 goto doip

goto help

:doip
netsh interface ip set address name="%1" source=dhcp
netsh interface ip set dns name="%1" dhcp
goto endp

:help
echo.
echo Usage: dhcpip interface-name
echo.
echo Sets specified network interface to DHCP mode
echo. 

:endp

You can use the batch files by passing the necessary arguments similar to the examples shown below:

C:\>staticip "Local Area Connection" 192.168.1.10 255.255.255.0
C:\>dhcpip "Local Area Connection"

ibrahim = { interested_in(unix, linux, android, open_source, reverse_engineering); coding(c, shell, php, python, java, javascript, nodejs, react); plays_on(xbox, ps4); linux_desktop_user(true); }