Set Your IP Address Via Batch File
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:
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:
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. ![]()
MMMMM... Related Posts
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!
[...] other day, I wrote a post on Setting Your IP Address Via Batch File. In this post, I’ll teach you how to reset your IP information from a different batch [...]
[...] Sadeghi demonstrates how to set your IP address via a batch file and how to reset your IP address via another batch [...]
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….
[...] shows us how to Set your IP Address Via Batch File (sorry … i’m a computer [...]
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.
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