How to change IP address using a Batch File:
Starting with Windows 2000, Microsoft has provided a powerful utility, Netsh, which lets you display and modify the network configuration of Windows computers. You can use the Netsh on the command line or in a batch file. Here’s an example of how you can create a batch file that changes the IP address of the local machine.
Netsh is a command-line scripting utility that allows you to, either locally or remotely, display or modify the network configuration of a computer that is currently running. Netsh also provides a scripting feature that allows you to run a group of commands in batch mode against a specified computer. Netsh can also save a configuration script in a text file for archival purposes or to help you configure other servers.
In my environment, we don’t use DHCP. We use static IP addresses. Using static IP addresses usually doesn’t present any problems because we rarely move desktops between locations. However, the people who use laptops usually visit multiple locations. At each location, they’ve been assigned a separate IP address. Each time they change location, they look up the appropriate network settings in a .txt file, then manually change those settings. This occasionally creates problems because they have to remember the correct steps to change their network settings and sometimes they mistakenly enter wrong numbers.
I found a solution. For each location, I created a simple batch file that the laptop users can run. Whenever they want to change their IP settings, all they have to do is execute the appropriate batch file.
The batch file uses the Netsh utility and contains below given commands.
Copy the code given below in the Notepad and save it with the .bat extension.
– Run the .bat file with double click.
– In the some cases, the .bat file needs to run with administrator’s privileges. Right-click on the .bat file and choose “Run as administrator“.
@echo off
ipconfig /flushdns
ipconfig /release
netsh interface set interface "Local Area Connection" disable
netsh interface ip set dns "Local Area Connection" static
netsh interface ip set address "Local Area Connection" static
echo Wait 5 seconds...
ping -n 60 -w 1000 0.0.0.1 > nul
netsh interface set interface "Local Area Connection" enable
netsh interface ip set address "Local Area Connection" static 192.168.XXX.XX 255.255.255.0 192.168.XXX.X
netsh interface ip set dns "Local Area Connection" static 192.168.XXX.XXX
To change the IP Address to “obtain an IP address automatically” or to remove the IP Address, Copy the code given below in the Notepad and save it with the .bat extension.
@echo off
ipconfig /flushdns
ipconfig /release
netsh interface set interface "Local Area Connection" disable
netsh interface ip set dns "Local Area Connection" dhcp
netsh interface ip set address "Local Area Connection" dhcp
echo Wait 5 seconds...
ping -n 60 -w 1000 0.0.0.1 > nul
netsh interface set interface "Local Area Connection" enable
Now, whenever the users want to change their IP settings, all they have to do is run the batch file.
Note: This entire process (IP Address change process) will take max 60 to 80 seconds.
Source: windowsitpro, sevenforums
Good one Madhu! Keep writing good article!
Thank you Sir, please suggest some topics, which are useful to everyone.
Hi Madhu,
I am searching for the same solution. changing my ip address periodically. But I wanted to so the same on my linux machine. Can you please help me achieving that.
Hi! Aswanth,
Very sorry for the late reply.
I am a Windows Administrator. I don’t know much about Linux OS.
Will it mess up my internet?
great it’s work perfectly madhu. last thing i want to know how to add alternate dns address using a Batch File Script
Highly descriptive blog, I loved that bit. Will there be a
part 2?
can you make one that work for every pc? without changing interface name?
is there a way to make this script to automatically run in the background so it refreshes the IP every minute???
Thank you
works good
thanks
Hello all, here every one is sharing such experience, thus it’s nice to read this web site, and I used to pay a visit this webpage everyday.
Hello Sir
I’d like to know, how I could change my IP address every time i connect dongle, using Javascript.
Please help me sir.
Superb solution……Thanks alot
Thank you Ashish. Sorry for the late reply
HI madhu.
Your given scripting is working good & i must say that now i have a solution for my long time problem. But Mr.Madhu, I cant set seconday DNS with this script. Do you have solution for this.
Hi! Harshil,
Thank you.
I will check and update you at the earliest.
Thanks sir
for sharing the IMP information
but how to configure wifi adapter this is working only lan
Thank you Shoyeb,
I will check and reply back to you at the earliest.
For new version of Windows 10 slight changes are there……use below code
@echo off
ipconfig /flushdns
ipconfig /release
netsh interface set interface “My internet connection” disable
set dnsservers name=”My internet connection” source=static
set address name=”My internet connection” source=static
echo Wait 5 seconds…
ping -n 60 -w 1000 0.0.0.1 > nul
netsh interface set interface “My internet connection” enable
netsh interface ip set address “My internet connection” static 192.168.XX.XX 255.255.255.XX 192.168.XX.XX
netsh interface ip set dns “My internet connection” static 192.168.XX.XX
echo Done..
Thank you very much Davis. I will update it.