How to change IP address using a Batch File Script on Windows?

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

 

21 thoughts on “How to change IP address using a Batch File Script on Windows?

      1. 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.

  1. great it’s work perfectly madhu. last thing i want to know how to add alternate dns address using a Batch File Script

  2. 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.

  3. 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.

  4. 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..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s