How to change IP address using script on Windows?

I need fixed IP in three wireless networks I log in frequently.

It is a pain to set those IP and gateway everytime I change networks.

I am willing to write a script to change my IP and gateway between those 3 fixed IPs and a choice to make it get IP automatically (when I´m in another network).

A plus feature to my script would be if it could discover the wireless network I am logged in and decide the IP automatically.

Is it even possible?

Thanks.

1

5 Answers

ipconfig /renew to get a new IP address or default gateway The NETSH command does this. check this.

See this example for setting DNS address manually via netsh command:

netsh interface ip set dns "Local Area Connection" static 192.168.0.200

configure your NIC to dynamically obtain its DNS settings (* Obtain DNS server address automatically):

netsh interface ip set dns "Local Area Connection" dhcp

and to set the * Obtain an IP address automatically setting:

netsh interface ip set address "Local Area Connection" dhcp

More info: read this Configure TCP/IP from the Command Prompt and How to change IP address from command prompt.

This is assuming 3 things.

1) The network adapter you're trying to change the IP for is "local area connection". It could also be "local area connection 2" or "wireless network connection". Look in your control panel for the correct name.

2) The IP you want to set is 192.168.0.101, change this to whatever IP to want to use.

3) The default gateway and dns are the same IP. If you are using some kind of router they usually are. Change this to match your network config found with the command ipconfig /all


Here is example of batch file that I have created for your problem solutions: just change the command after the con1: type labels

How to create batch file: paste this code in notepad and save it as "test.bat"

Note: Change your connection names "local area connection" to your "wireless connection" name

@ECHO off
cls
:start
ECHO.
ECHO 1. Change Connection1 Static IP
ECHO 2. Change Connection2 Static IP
ECHO 3. Change Connection3 Static IP
ECHO 4. Obtain an IP address automatically
ECHO 5. Exit
set choice=
set /p choice=Type the number to print text.
if not '%choice%'=='' set choice=%choice:~0,1%
if '%choice%'=='1' goto con1
if '%choice%'=='2' goto con2
if '%choice%'=='3' goto con3
if '%choice%'=='4' goto autosearch
if '%choice%'=='5' goto end
ECHO "%choice%" is not valid, try again
ECHO.
goto start
:con1
ECHO Connecting Connection 1
netsh interface ip set address "Local Area Connection" static 192.168.0.10 255.255.255.0 192.168.0.1 1
goto end
:con2
ECHO Connecting Connection 2
netsh interface ip set address "Local Area Connection1" static 192.168.0.10 255.255.255.0 192.168.0.1 1
goto end
:con3
ECHO Connecting Connection 3
netsh interface ip set address "Local Area Connection2" static 192.168.0.10 255.255.255.0 192.168.0.1 1
goto end
:autosearch
ECHO obtaining auto IP
ipconfig /renew "Local Area Connection"
goto end
:bye
ECHO BYE
goto end
:end

Hope this help you.. for more reference check this Set Your IP Address Via Batch File

3

You can make a .bat script file that contains the configuration commands based on "netsh" command, for example:

This changes the IP address and Default Gateway:

"netsh int ip set address "local area connection" static {192.168.0.101} 255.255.255.0 {192.168.0.254} 1"

This changes DNS:

"netsh int ip set dns "local area connection" static {192.168.0.254} primary"

Run this on CMD as admin:

to static:

netsh interface ip set address name = "Local Area Connection" static 192.168.1.1 255.255.255.0

back to dhcp:

netsh interface ip set address name = "Local Area Connection" dhcp

"Local Area Connection" can be different in your case

I know this is very old thread but still it can be useful to many like me.

As per the op asked for WiFi, I have created a batch script for Wi-Fi. It will fetch current Wi-Fi connected and based on that set Static IP or DHCP.

Here is the script -

@echo off
Ping -n 1 -w 1000
cls
if errorlevel 1 ( if not "%1"=="am_admin" (powershell start -verb runas '%0' am_admin & exit /b) :start for /f "delims=: tokens=2" %%n in ('netsh wlan show interface name="Wi-Fi" ^| findstr ^/R "\<SSID"') do set "Network=%%n" set "Network=%Network:~1%" IF "%Network%"=="Home" goto DHCP ELSE IF "%Network%"=="OfficeWifi1" goto Static ELSE IF "%Network%"=="AnotherOffice2" goto Static2 ELSE IF "%Network%"=="OpenWifi" goto DHCP ELSE goto DHCP goto end :Static set IP=192.168.1.118 set Sub_Mask=255.255.255.0 set D_Gate=192.168.4.4 set DNS=192.168.4.1 set DNS2=192.168.4.2 netsh interface ip set address "Wi-Fi" static %IP% %Sub_Mask% %D_Gate% 1 netsh interface ipv4 set dns name="Wi-Fi" static %DNS% netsh interface ip add dns name="Wi-Fi" addr=%DNS2% index=2 netsh int ip show config goto end :Static2 set IP=192.168.2.201 set Sub_Mask=255.255.255.0 set D_Gate=192.168.4.4 set DNS=192.168.4.1 set DNS2=192.168.4.2 netsh interface ip set address "Wi-Fi" static %IP% %Sub_Mask% %D_Gate% 1 netsh interface ipv4 set dns name="Wi-Fi" static %DNS% netsh interface ip add dns name="Wi-Fi" addr=%DNS2% index=2 netsh int ip show config goto end :DHCP netsh int ip set address name = "Wi-Fi" source = dhcp netsh interface ipv4 set dns name="Wi-Fi" source="dhcp" goto end :end msg * "WLAN wise IP is set up"
)

You should save this a batch file and run that batch file as %% in for command will not work in command prompt.

TIP:You can also add this in Task Scheduler with Trigger as below and Action with above batch path

Trigger : On an event
Log: Microsoft-Windows-NetworkProfile/Operational
Source: NetworkProfile
Event ID: 10000

Update

In Windows 10 & 11, you can set the IP configuration as well as DHCP / Static settings for each Saved WiFi network also.

in this cmd how to set dns netsh interface ip set address name = "Local Area Connection" static 192.168.1.1 255.255.255.0

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like