Search code examples
windowsbatch-filenetwork-programmingreset

Remove Static IP & DNS on all Interfaces - Batch


I need a Window's batch script that will remove the static IP address (Change to DHCP), Remove the set DNS, and enable all the network interfaces without having to specify the interface name; since some interfaces may be named differently.


Solution

  • I created this last night, and everything seems to work the way it should.

    @echo off
    
    : Disable Static IP/Enable DHCP    Remove DNS     Enable NICs
    for /f "skip=2 tokens=3*" %%i in ('netsh interface show interface') do (
    netsh int ip set address "%%j" dhcp >nul 2>&1
    netsh int ip set dns "%%j" dhcp >nul 2>&1
    netsh interface set interface name="%%j" admin=enabled >nul 2>&1
    )
    

    If you want to take it a step further and clear the DNS Cache, add this to the next line.

    ipconfig /flushdns >nul 2>&1