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!
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….