I'm trying to write a script that will involve modifying the IPv4 configuration of an interface. The interface may or may not already have any of the following configured:

  • IP Address
  • Subnet Mask
  • Default Gateway
  • DNS Servers
  • WINS Servers

When my script is done, I want only the options that I have set to be configured. (e.g.: If I don't set a Gateway, DNS, or WINS server, then I want none to be configured.) I figure the easiest way to ensure this is to wipe out the existing configuration as much as I can and then build up just what I need.

Leaving the existing IP Address and Subnet Mask is okay, because these will definitely be overwritten every time. It looks fairly simple to delete DNS and WINS servers with netsh.

netsh interface ipv4 delete dns "LAN" all
netsh interface ipv4 delete wins "LAN" all

My problem comes with deleting the gateway. It seems that every netsh command that is used to modify the default gateway requires the current IP address as a parameter. However, this will not always be something that is known. Is there not a way to use netsh to just delete all the gateways configured for an interface, without specifying the IP?

  • 1
    What version of Windows? – Darth Android Aug 13 '13 at 15:23
  • Are you willing to set a new IP/mask? – Jack Aug 13 '13 at 15:29
  • Windows 7/2008. And part of the script's purpose is to set a new IP & Subnet in the first place. So, I think I just realized my answer. – Iszi Aug 13 '13 at 15:56

Leaving the existing IP Address and Subnet Mask is okay, because these will definitely be overwritten every time.

Since you're overwriting the IP and Subnet Mask anyway, just use netsh to delete the gateway in the same step. e.g.:

netsh interface ipv4 set address "Local Area Connection" static none

Here, you place none where the Default Gateway would normally go. This specifies that no gateway will be used with this IP.

  • @lszi "Local Area Connection" - unknown interface? – STTR Aug 13 '13 at 17:08
  • @STTR Your interface may be named differently. Use netsh interface ipv4 show interfaces to find the correct name. Also make sure to replace and with the actual IP Address and Subnet Mask you want to use on that interface. – Iszi Aug 13 '13 at 18:09
  • this is ok if you only have 1 NIC... of course you can repeat for every NIC... nevertheless not OK if you rely on DHCP... missing a better way I would endorse the solution presented next by STTR, relying in route command – ZEE Mar 19 '18 at 18:28

Delete default gateway:

route delete -p

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.