Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


Return to article

Making IP Address Changes from Within a Script
 

One of the Windows 2000 features that I've found most useful is the capability to change from dynamic to static IP addressing on a workstation without needing to reboot. The ability to execute the change from within a script would make this new feature even more useful. Is this type of execution possible?

To make IP address changes from within a script, you can use Win2K's multipurpose Net Shell (Netsh) command. This command provides several functions that relate to viewing and changing IP addressing on a Win2K system. For example, to change a system from static IP addressing to dynamic DHCP-based addressing, open a command prompt and enter the following command:

netsh interface ip set address "<connection name>" dhcp

where connection name is the name of the LAN adapter on which you're making the IP address configuration change. (By default, this adapter is called local area connection, but it might have a different name on machines with multiple LAN adapters or on machines that an administrator has manually renamed.)

To make the reverse change—that is, to change a connection or adapter from DHCP-configured addressing to static addressing—enter the following command:

netsh interface ip set address "<connection name>" static <ip_address>
<netmask> <gateway> <metric>

where ip_address is the static IP address you wish to assign to the connection, netmask is the subnet mask (e.g., 255.255.255.0) associated with the IP address, gateway is the default gateway (if any) on the local IP subnet, and metric is an optional numeric value (e.g., 1, 2) that defines the number of hops to the default gateway (if one exists). For example, to change from DHCP-based addressing to the static IP address 10.1.1.2, mask 255.255.255.0, and gateway 10.1.1.1 (metric 1), you'd enter

netsh interface ip set address "local area connection" 
static 10.1.1.2 255.255.255.0 10.1.1.1 1






Reader Comments

Just what I was looking for, THANKS!!

Howard Davis -November 10, 2003

How do you change the IP address of a remote system on the network using the same command?

Anu Skariah -November 12, 2003

To change the IP address of a remote system on the network ad the required IP address in same command and then the same command can be made to run at the remote system by adding it to the schduler of the remote system.

Deepak kumar -November 17, 2003

This is great. Thanks

Sean Lee -March 29, 2004

Really great! But what if you need to specify statis DNS settings? What do you add to the command?

Donovan -April 11, 2004

This sounds very good, is it possible to use this with script logons to allow or disable internet access? I have a domain with 3 Ou's but only one of them should have access to the internet , no matter what machine they use... maybe it's possible to assign a logon script using Group Policy to change the default gateway?? (!)

Bruno Quintas -April 20, 2004

For dns settings i think it would me netsh interface ip set dns

... I havent tried this yet but it looks correct. Ex. set dns "Local Area Connection" static 10.0.0.1

Sid -April 21, 2004

How can change the subnetmask, the rest should not be changed

patrick -May 05, 2004

It works great! Thanks to the author

martin -June 03, 2004

That is what i was looking for, Made my life eassy. Thanks a lot !!

Surej -July 06, 2004

What do we need to enter for multiple DNS/WINS address entries? I.E. Primary and Alternate...we are using four address for DNS and WINS...

carleokj -September 21, 2004

to add additional dns and wins netsh interface ip add dns "local area connection" 3.3.3.3 netsh interface ip add wins "local area connection" 3.3.3.3

Anonymous User -October 21, 2004

Can someone tell mehow I remove the DNS settings (to obtain automatic) when i use netsh to set dhcp

Anonymous User -October 28, 2004

It all works pefectly exept the removal of the dns. When i use the netsh dhcp > it doesnt change my dns settings to automatic .. how do i do that?

Anonymous User -October 28, 2004

To obtain a dynamic DNS, use: netsh interface ip set dns "Local Area Connection" dhcp

Anonymous User -October 31, 2004

How do i change 1 static ip address to another static ip?

Anonymous User -November 08, 2004

If you want to add multiple DNS and/or WINS addresses, just use something similar to this: netsh interface ip add dns "Local Area Connection" 10.10.10.1 netsh interface ip add dns "Local Area Connection" 10.10.10.2 netsh interface ip add wins "Local Area Connection" 10.10.10.1 netsh interface ip add wins "Local Area Connection" 10.10.10.2 This should give you two DNS and two WINS addresses.

Anonymous User -November 16, 2004

I've been looking for this. Its great.

Anonymous User -November 21, 2004

Great !!! Thanks !!!

Anonymous User -November 24, 2004

How can I make a script of this to run when I double click a file instead of having to open a command window each time?

Anonymous User -December 01, 2004

To create a script, just use notepad and save the file as static.cmd for example.

Anonymous User -December 05, 2004

How do I script in a Gateway IP?

Veece -December 17, 2004

Is there anyway to set the logon script to search for a dhcp, but in none is found to set a static ip.

Anonymous User -January 21, 2005

i'm looking for the same thing. if there is no dhcp i want to set the ip address. This sure would be helpful

Anonymous User -January 21, 2005

Has anyone figured out if you can be prompted for input? I am changing my entire network and have over 100 statically assigned machines, I would love to be able to run a batch file and just have to enter the last octet of the IP. Any idea how I could do that?

Anonymous User -January 26, 2005

What if you have a variable naming standard for the various interfaces instead of "Local Area Connection"? Is there a way to change DNS addresses for all interfaces regardless of the names?

Anonymous User -January 28, 2005

Thank you knidly for this :)

Anonymous User -February 10, 2005

How do you delete an IP, DNS, or WINS entry before adding the new address?

Anonymous User -February 10, 2005

What if you have a variable naming standard for the various interfaces instead of "Local Area Connection"? Is there a way to change DNS addresses for all interfaces regardless of the names? Yep yep... this would be very handy... Dan..

Anonymous User -February 21, 2005

how can change only subnet mask? Thanks

Anonymous User -March 04, 2005

How do I change my IP address, i have already enabled dhcp... so now what can i do to change the original IP address. Im doing this becuase I was banned from an online game for stupid reason, and want to get back in, but they said "we traced your IP and we are permanetly banning you"... please help me out. Thanks.

Anonymous User -March 06, 2005

To delete all WINS settings: netsh interface ip delete wins "Local Area Connection" all

Anonymous User -March 17, 2005

Perfeito este documento, é muito bom! ***: Dimie - BRASIL

Anonymous User -March 23, 2005

Good articel

Anonymous User -May 13, 2005

How can i do this changes from static to dhcp on a win NT workstation?

tecnidi -June 27, 2005

super ...

Anonymous User -July 01, 2005

Its realy great. Can someone tell how to add additional gateway address

Anonymous User -July 19, 2005

The script works perfectly... but I still have one request: what do I have to add, so that netsh is closed and i return to the ususal "C:\>"? I will do it remotely on another machine at startup, so it should close automaticaly.

Anonymous User -July 20, 2005
Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement