Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Good Morning,
We are moving from a DHCP environment to a static environment and the prospect of touching each machine (600+) does not sound nice. Is there a way to capture the current IP address assigned via DHCP and netsh it into a static IP?
thanks for looking at this post

I hope this helps point you in the right direction. I am not on a works network as you are this is for my home computer. I run this once a week to give me a week on the Internet. I have ipconfig /all twice so I can see whether I have been assigned a week, if not I run it again. I also have to overcome the slight problem of the first time the command:-
netsh interface ip set address local static %IP% %Subnet% %Default% 1 is run the Default gateway does not always take so I run it twice to make sure. All that said it works and has done for many years, it can be improved maybe someone will help.@echo off
sc config Dhcp start= auto
net start Dhcp /y
netsh interface ip set address "local area connection" dhcp
@ipconfig /release *CON*
@ipconfig /flushdns
@ipconfig /renew
@ipconfig /registerdns
@ipconfig /all >%systemroot%\ipconfig.txt
@ipconfig /all
net stop Dhcp /y
sc config Dhcp start= disabled
for %%a in (%systemroot%\ipconfig.txt) do find "IP Address" <%%a>> %systemroot%\ipconfig1.txt
for /f "tokens=2 delims=:" %%b in (%systemroot%\ipconfig1.txt) do set IP=%%b
for %%c in (%systemroot%\ipconfig.txt) do find "Subnet" <%%c>> %systemroot%\ipconfig1.txt
for /f "tokens=2 delims=:" %%d in (%systemroot%\ipconfig1.txt) do set Subnet=%%d
for %%e in (%systemroot%\ipconfig.txt) do find "Default" <%%e>> %systemroot%\ipconfig1.txt
for /f "tokens=2 delims=:" %%f in (%systemroot%\ipconfig1.txt) do set Default=%%f
netsh interface ip set address local static %IP% %Subnet% %Default% 1
netsh interface ip set address local static %IP% %Subnet% %Default% 1
CD.>%systemroot%\ipconfig1.txt
pause
shutdown -r -f -t 00 & exit

That worked like a charm. I had to do very little to it (add a DNS value, basically) to make it work here. I'm going to test it in a startup script and then I think my problems are over!
thank you sooo much, you saved me a ton of trekking!

That might be an expedient approach, but it's a bad approach.
You should do static dhcp so that you can control the addressing at the server rather than at the workstation.

Hi,
For the DNS portion, how do you modify the script to grab the DNS from the ipconfig.txt file and set it static in the DNS portion?

As you can see from my original postings I can only give an idea from home point of view. I have had a quick look and this works. You may have to adapt of course
@echo off & setLocal EnableDelayedExpansion
sc config Dhcp start= auto
net start Dhcp /y
netsh interface ip set address "local area connection" dhcp
@ipconfig /release *CON*
@ipconfig /flushdns
@ipconfig /renew
@ipconfig /registerdns
@ipconfig /all >%systemroot%\ipconfig.txt
@ipconfig /all
net stop Dhcp /y
sc config Dhcp start= disabled
for %%a in (%systemroot%\ipconfig.txt) do find "IP Address" <%%a>> %systemroot%\ipconfig1.txt
for /f "tokens=2 delims=:" %%b in (%systemroot%\ipconfig1.txt) do set IP=%%b
for %%c in (%systemroot%\ipconfig.txt) do find "Subnet" <%%c>> %systemroot%\ipconfig1.txt
for /f "tokens=2 delims=:" %%d in (%systemroot%\ipconfig1.txt) do set Subnet=%%d
for %%e in (%systemroot%\ipconfig.txt) do find "Default" <%%e>> %systemroot%\ipconfig1.txt
for /f "tokens=2 delims=:" %%f in (%systemroot%\ipconfig1.txt) do set Default=%%f
for /f "tokens=*" %%g in (%systemroot%\ipconfig.txt) do (
set /a N+=1
if !N! ==21 echo %%g >> %systemroot%\ipconfig1.txt
if !N! ==22 echo %%g >> %systemroot%\ipconfig1.txt
)
for /f "tokens=2 delims=:" %%h in (%systemroot%\ipconfig1.txt) do set DNS1=%%h
for /f %%i in (%systemroot%\ipconfig1.txt) do set DNS2=%%i
netsh interface ip set address local static %IP% %Subnet% %Default% 1
netsh interface ip set address local static %IP% %Subnet% %Default% 1
netsh interface ip add dns "local area connection" %DNS1%
netsh interface ip add dns "local area connection" %DNS2% index=2
CD.>%systemroot%\ipconfig1.txt
pause
shutdown -r -f -t 0 & exit

Yes, that does work.
One caveat here is that all your network devices are called
"local area connection"
if for some reason you've had a nic go bad or something else
and you have a machine with "local area connection 2" or
beyond, this will not implement on those nics.However, beyond that it works very well. Obviously this is not a very managed approach to configuring a network, but it does exactly what is advertised. It takes dynamically assigned network values and converts them to static.
A very neat little script that does the job.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |