You all know that I love the Command Prompt and creating fanciness through Batch Files – and here’s a good one for you: Set your IP address via a batch file!
This command will work in both Windows XP and Windows Vista
Why Should I Bother?
This can be useful for a few reasons:
- You use DHCP at home and a Static IP at work (assuming you transport your laptop)
- You need to set the IP address for multiple computers
- You are in a hotel
- etc
Basically, if you transport your laptop into different environments that require different network settings.
How To
I’ll first post the code, then I’ll explain what’s going on.
The code:
-
@ECHO OFF
-
-
set varip=65.88.48.77
-
set varsm=255.255.255.224
-
set vargw=65.88.48.65
-
set vardns1=66.78.202.254
-
set vardns2=66.78.210.254
-
set varhome=www.google.com
-
-
REM ***** You don’t need to change anything below this line! ******
-
-
ECHO This fanciness is brought to you by Saman Sadeghi!
-
ECHO Setting IP Address and Subnet Mask
-
netsh int ip set address name = "Local Area Connection" source = static addr = %varip% mask = %varsm%
-
-
ECHO Setting Gateway
-
netsh int ip set address name = "Local Area Connection" gateway = %vargw% gwmetric = 1
-
-
ECHO Setting Primary DNS
-
netsh int ip set dns name = "Local Area Connection" source = static addr = %vardns1%
-
-
ECHO Setting Secondary DNS
-
netsh int ip add dns name = "Local Area Connection" addr = %vardns2%
-
-
ECHO Setting Internet Explorer Homepage to %varhome%
-
reg add "hkcusoftwaremicrosoftinternet explorermain" /v "Start Page" /d "%varhome%" /f
-
-
ECHO Here are the new settings for %computername%:
-
netsh int ip show config
-
-
pause
Now, I realize that it looks like there is a lot going on there but there really isn’t. The way I have written the code, the user will not see all of the coding when the batch file is run, but they will see feedback as to what is going on. Also, you don’t really have to declare all of the variables first, you could just pass them directly from that line. Example:
-
netsh int ip set address name = "Local Area Connection" source = static addr = 65.88.48.77 mask = 255.255.255.224
But, I created this so that you (or whoever you give this to) wont have to go searching through the code if the variables need to be changed – plus, it’s better programing practice to declare your variables first!
How To I Use This Code?
Open a new text file and copy/paste the code, then save the file as a .bat. You will need to change all of the variables (the six at the top of the code) to meet your networking needs!
Download
You can download the raw code – again, you must save the file with a .bat extension.
What’s Going On?
It really is pretty self explanatory, all of the netsh int commands set the corresponding network connection information. The second-to-last command (the one for setting the home page) sets a registry key with the value that you declared earlier (%varhome%). It may not be necessary for your application, but I thought I should include it for you anyway!
How To Reset To DHCP
I have written another article and posted the code to Reset Your IP Address Via Batch File.
Now this is quite ‘choice’. There is beauty in the simplicity of batch files – and to set IP address – it’s so very nice. Saman – thanks, I’ll have to try this out when I get back home.
We missed you in ASCO_Chicago. Wish you were here!
Thanks Ron!
I did wish I was there, but only to see everyone – I didn’t want to deal with the 500+ cases of equipment!
Yo Saman!!
I like the idea of this batch file. However, it is not working for me. I get errors….”Invalid interface oLocal specified”…whats up with that?? I get this with both of your .bat files….setting and resetting my IP.
It took me a while but I finally figured out the problem:
For some reason, when you copy/paste or download the code, the quotation marks are changed and the Command Prompt can’t understand them!
Just replace all of the quotes by retyping them… or just copy/paste the code again, I have fixed the problem on my end!
Problem I found was that my Local Area Connection was not name that.
e.g. If Local Area Connection is called Sandra then the first line of this would have to be:
netsh int ip set address name = "Sandra" source = static addr = %varip% mask = %varsm%Nice code….very much like
I see the line of code that changes the homepage. Searching for ways to change the homepage via the command line brought me here. IE7 has the ability to have multiple homepages. How can one set that up via the sting of code you used in your script? I found the registry setting after I set up a couple home pages but there was only one address in there – not multiples separated by commas or anything.
Any help would be cool.
By the way, the current code gave me errors. It should be:
reg add “hkcu\software\microsoft\internet explorer\main” /v “Start Page” /f /d “%varhome%”The /f switch should come before the /d otherwise you may see an error like:
“Error: Too many command-line parameters”
That’s odd, I’m not getting that error….
Please can you show us how it work with Windows NT
I’ve been at this all day. I have tried 3 different methods of setting a static ip in a network card on Vista. For some reason, every one of them runs without an error, but when I check the settings they have changed everything but the ip and mask. What am I missing here?
Do anyone have this for vista. It works with XP but not vista
I know this is off topic but I am wondering if there is a way to create a .bat to modify a reg string for example ( HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Dhcp and modify the dependonservice ) Norton likes to put a entry in it. I want to creat a bat program that will remove the SYMTDI entry out of it. Any help would be great.
Very good! Just what i wanted.
Just one problem: how do you write a patch that will make my laptop obtain IP and DNS automatic?
Do you put 0.0.0.0?
Thank you
Bayram
I’m trying to create a completely touches imaging process using sysprep, wsname and netdom. So far, I can successfully rename a computer based on a text file with MAC addr and comp name. I can also add it to the domain. Do you know of a way to assign static IPs (via batch file) using a text file with MAC addresses or computer name and the appropriate IP address?
VISTA UPDATE!!!!!
FINALLY! I have worked out how to get it to work in vista!!
@ECHO OFF
set varip=65.88.48.77
set varsm=255.255.255.224
set vargw=65.88.48.65
set vardns1=66.78.202.254
set vardns2=66.78.210.254
REM ***** You don’t need to change anything below this line! ******
ECHO This fanciness is brought to you by Saman Sadeghi!
ECHO Setting IP to static
netsh int ipv4 set address name = “Wireless Network Connection” source = static address = %varip% mask = %varsm%
ECHO Setting IP Address and Subnet Mask
netsh int ipv4 set address name = “Wireless Network Connection” address = %varip% mask = %varsm% gateway = %vargw% gwmetric = 1
ECHO Setting Primary DNS
netsh int ip set dns name = “Wireless Network Connection” source = static addr = %vardns1%
ECHO Setting Secondary DNS
netsh int ip add dns name = “Wireless Network Connection” addr = %vardns2%
ECHO Here are the new settings for %computername%:
netsh int ip show config
pause
The change is quite simple…the problem seems to lie in vista’s inability to set the ip to static, and assign the ip address at the same time. So all you have to do is set up the ip to static first, then set the ip…very strange
Note that your static ip is not displayed when shown, however it is set.
Dear / Sir,
My name is Henry Ng, i had copy paste the command and save into bat file format and it cannot set the ip address.
error msgs: invalid ip interface. please advice. Thank you.
Great work all i now need is to specify a printer
hi
iam useing 192.168.1.1 and subnetmask is 255.255.255.0 and for internet is 9.2.2.2 and subnetmask is 255.0.0.0
i like to add the second ip through netsh command
when i run the batch program “Set Your IP Address Via Batch File” no ip address and subnet musk appear. any idea or solution?
how to set / change ip address randomly in batch mode
rem [new ip address]
rem [old varip is "set varip=65.88.48.77"]
rem [change to]
set varip=65.88.48.
rem [other same line]
call xset32 VARIPNEW RANDOM 0 255
rem [old netsh int ip set address name = "Local Area Connection" source = static addr = %varip% mask = %varsm%]
netsh int ip set address name = “Local Area Connection” source = static addr = %varip%%VARIPNEW% mask = %varsm%
rem [etc]
==
note : xset32/xset from hxxp://xset.tripod.com/latest.htm
Thanks! This has been very helpful to me!!!
i have tried and goes like this
“invalid Interface Local Area Connection”. whats that mean???
Where should i find my source address???
If you get ‘Invalid interface …’ and you have specified the correct interface name in the netsh command line then it’s probably the remote registry service that isn’t running. ‘netsh interface ip show address’ will display an interface name. If the name is a GUID like ‘{CCFCBE35-812F-49F6-9902-43C767776522}’ then again, it’s probably the remote registry service that isn’t running. Use ‘net start remoteregistry’ to start it.
“Local Area Connection” is the name of the connection in your list of Network Connections (gotten to via the Control Panel). You need to figure out which connection you wish to change via the batch file and change the name accordingly. Meaning, look in the Network Connections list and find what NIC you would normally change the properties of. Note the name of it. In the batch file, everywhere it says “Local Area Connection” should be changed to the name of the connection you wish to change. I have 2 NICs in my PC. By default, they get named, “Local Area Connection” and “Local Area Connection 2″ by I always rename them so I know what they are. In my case, I’ve renamed them “ON-Board NIC” & “PCI NIC” so I know which is which.
Excellent Work …got my problem solved ..As i often have to move b/w two offices both having different IPs ..good job ..
It is awesome man………
Keep going……….god bless you……..
I found that this didn’t work quite right on vista – the gateway line overwrites the IP address (Razzor almost has it right, but it’s the overwriting that’s the problem, rather than “change-to-static-then-assign-the-ip”) as they are both setting the “address” parameter. So this should work:-
ECHO Setting IP Address, Subnet Mask and Gateway
netsh int ip set address name = “Local Area Connection” source = static addr = %varip% mask = %varsm% gateway = %vargw% gwmetric = 1
and also, you can change the “Local Area Connection” to “Wireless Network Connection” for wireless adaptor settings – if you run the command
netsh int ip show config
it will show you the relevant adaptor names for your system.
You will need to run this in elevated mode. I set this up by creating a desktop shortcut, then right-click it, [Properties] then on the [Shortcut] hit [Advanced] and there is a [Run as Administrator] option you can tick that will invoke the normal elevated privileges prompt.
Anybody know if there is any way to take the router assigned dhcp IP/dns settings in your LAN/Wireless and automatically convert to a fixed IP address with the same Ip/dns address? ie. read the current dhcp IP/dns, put into variables, set to static LAN using the variables?
Is there any way to set up and if/else statement for it to automatically change the IP based on the ssid of the router you are connected to?
such as:
IF ssid = workthen
:::code for set static IP:::
else
::::code for set to dhcp::::
@rem echo OFF
set lan_name=”Local Area Connection”
rem define all the network addresses here
set subnet=192.168.1
set lan_ip=%subnet%.250
set sub_net_mask=255.255.255.0
set gateway_addr=%subnet%.64
set pri_dns_addr=%subnet%.14
set sec_dns_addr=4.2.2.2
rem set the ip address
@netsh interface ip set address name=%lan_name% source=static addr=%lan_ip% mask=%sub_net_mask% gateway=%gateway_addr% gwmetric=1
rem set the DNS
@netsh interface ip set dns name=%lan_name% source=static addr=%pri_dns_addr% primary
rem add an alternative DNS (optional)
@netsh interface ip add dns name=%lan_name% addr=%gateway_addr% index=2
Thanks allot Saman
Is there a way of doing this whereby the batch file will work across different PC’s with different LAN names? I have lots of different machines to change the IPs on but they often have different LAN names. I just need to change the IP on the currently connected LAN.
Cheers.
I believe this has to be done locally.
That’s OK, the batch file will be run locally, I just don’t want to keep having to change it for each different PC depending on what LAN name it is using. Some of the PC’s have the LAN connection called “Local Area Connection” whereas some have it called “Corporate Network” or “Local Area Connection 1″. Is there a way of doing this?
Thanks in advance.
Hello,
forgive me, but I am new to the Vista world, could you clearify. I Just want to change the local IP on my vista IPV4 machine to a static IP. I would only need this for local area connections. what .bat file would I use, I see a few variations.
Thanks in advance.
WOW. U the MAN..
Hello,
can u tell me how to make a batch file which resets my ip till the ip-range is ok?
i want the ip range to 92.xxx.xxx.xxx
what i do now is connect the dialup(ppoe), then i check the ip range and see its 85.177.xxx.xxx or 78.54.xxx.xxx, i disconnect, and then i connect again and hope its the ip range 92.xxx…
i get the range sometimes. in the evening very fast, because provider is busy, but nights or mornings it takes ages and thousands of clicks.
hope you can help me
patrick
This works and sets the ip etc. but when I restart the pc it drops off. I need to going into the LAN Properties and open up the TCP/IP properties -> advanced and just ok out followed by another restart to get it back online.
Thoughts?
@ECHO OFF
set varip=10.199.1.223
set varsm=255.255.255.0
set vargw=10.199.1.179
set vardns1=202.54.9.1
set vardns2=202.9.145.6
REM ***** You don’t need to change anything below this line! ******
ECHO This fanciness is brought to you by Susanta Naskar!
ECHO Setting IP Address and Subnet Mask
netsh int ip set address name = “Local Area Connection” source = static addr = %varip% mask = %varsm%
ECHO Setting Gateway
netsh int ip set address name = “Local Area Connection” gateway = %vargw% gwmetric = 1
ECHO Setting Primary DNS
netsh int ip set dns name = “Local Area Connection” source = static addr = %vardns1%
ECHO Setting Secondary DNS
netsh int ip add dns name = “Local Area Connection” addr = %vardns2%
reg add “hkcusoftwaremicrosoftinternet explorermain” /v “Start Page” /d “%varhome%” /f
ECHO Here are the new settings for %computername%:
netsh int ip show config
pause
Its Great man Thanks for the stuff
Thanx. Your script gave me all the commands I needed to make my own code
hi,
is there a way to disable changing the ip address?
thanks
I need a batch command or code that gives me any random ip, prefer one that can give me one in a range of 100 to 223 for xp,
I need a ipconfig /release command to start off and a dhcp command to end the code
also do you know a batch code or command to loop a batch file a specified number of times
I try to run it in windows 7, but it doesn’t work. May i get some advice? Thanks
Thanks man!!!
You saved me hours and hours of work!!!
when i enter this command i get…”dhcp is already enabled on this interface” when it is static
can anyone help me out pleasee
This worked for me in Win7 also added 2 lines to add WINS
@ECHO OFF
set varip=192.168.10.153
set varsm=255.255.255.0
set vargw=192.168.10.254
ECHO Setting IP to static
netsh int ipv4 set address name = “Local Area Connection” source = static address = %varip% mask = %varsm%
ECHO Setting IP Address and Subnet Mask
netsh int ipv4 set address name = “Local Area Connection” address = %varip% mask = %varsm% gateway = %vargw% gwmetric = 1
ECHO Setting Preferred DNS server
netsh int ip set dnsservers “Local Area Connection” static 192.168.10.4
ECHO Setting Alternate DNS server
netsh int ip add dnsservers “Local Area Connection” 192.168.10.22
ECHO Setting Primary WINS server
netsh int ip set winsservers “Local Area Connection” static 192.168.10.4
ECHO Setting Secondary WINS server
netsh int ip add winsservers “Local Area Connection” 192.168.10.22
pause
Working on Windows 7
Replace your network settings with these.
IP–> Subnet Mask –> Gateway is the order for first line of the batch file.
“Wireless” is the name of my Wireless network adapter, to find out the names of your adapters type ipconfig /all at command prompt. If you want to use your Ethernet Adapter (Wired) Fill in the name of that between quotes usually “Local Area Connection”
Good Luck!
________________________________________________________________
@ECHO OFF
ECHO Setting IP to static
netsh int ipv4 set address name=”Wireless” static 192.168.1.50 255.255.255.0 192.168.1.1
ECHO Setting DNS to static
netsh int ipv4 set dnsservers “Wireless” static 24.92.226.40
pause
Pls i dont knw programming.
I wil like to knw where to start so that i can change my IP using the .bat file.
I adapted this to use gpedit.msc to create a “permanently static IP address.”
In startup scripts:
command #1-
%windir%\system32\netsh.exe
int ip set address name = “Local Area Connection” source = static addr = 192.168.1.15 mask = 255.255.255.0 gateway = 192.168.1.1 gwmetric = 0
command #2-
%windir%\system32\netsh.exe
int ip set dns name = “Local Area Connection” source = static addr = 192.168.1.2
command #3-
%windir%\system32\netsh.exe
int ip add dns name = “Local Area Connection” source = static addr = 192.168.0.1
Why? Because I want to be able to reset the networking without losing the static address. To reset, I run these three commands in a bat file:
netsh int ip reset c:\dump.txt
netsh winsock reset
shutdown -r -f -t 20
anthony-
Make the user a “power user” and then use gpedit.msc to change user rights so that “power users” cannot change networking.
or
Make the user just a normal user.
Thanks @Art…
Thanks a ton. The Vista script helped me configure ip address on Vista. Copying and pasting script can cause some problems, you need to remove spaces around = and if quotes character is tilted then you need to replace it with actual quotes character.
For those getting ‘Invalid interface…’ you need to be careful how you copy/paste/edit the file. The
” (shows up as CURLY quote or raised double comma)
is not the same as:
” (shows up as straight quote)
The CURLY quote does NOT work; the straight ones do.
hi all.Please how can i change “Only” My Local Area Connection” gateway “Only” using patch file?
means i dont want the ip to be changed and the submask?only patch file to change the gatway any help pls?iam using Win7
thanks