Asked
Updated
Viewed
2k times

On Windows, I want to make a script that can change the networking configuration including:

  1. IP Address
  2. Subnet
  3. Gateway
  4. DNS1 and DNS2

Is it possible for the script to also query the gateway to be able to calculate the most appropriate values to use?

add a comment
1

1 Answer

  • Votes
  • Oldest
  • Latest
Answered

With the help of scripts or a series of short commands, there are numerous ways you can set the IP Address, subnet, gateways, and DNS servers for utilization. These commands will typically be run from the command console which you can reach in Windows by going to the start menu and typing cmd and then clicking on the program that says Command Prompt.

IPConfig

First, with the IPConfig command you can query to figure out your current network configuration:

ipconfig /all

This will output something like this:

C:\Users\Brian>ipconfig /all

Windows IP Configuration

   Host Name . . . . . . . . . . . . : Computer-Name
   Primary Dns Suffix  . . . . . . . :
   Node Type . . . . . . . . . . . . : Hybrid
   IP Routing Enabled. . . . . . . . : No
   WINS Proxy Enabled. . . . . . . . : No

Ethernet adapter Ethernet:

   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : Intel(R) Ethernet Connection (2) I219-V
   Physical Address. . . . . . . . . : 38-A7-E6-15-0A-A5
   DHCP Enabled. . . . . . . . . . . : Yes
   Autoconfiguration Enabled . . . . : Yes
   IPv6 Address. . . . . . . . . . . : 2401:602:187f:2600::191(Preferred)
   Lease Obtained. . . . . . . . . . : Monday, February 21, 2022 1:44:38 PM
   Lease Expires . . . . . . . . . . : Friday, February 25, 2022 8:27:38 AM
   Link-local IPv6 Address . . . . . : fe80::a051:86b:a18:ba77%3(Preferred)
   IPv4 Address. . . . . . . . . . . : 10.0.1.50(Preferred)
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Lease Obtained. . . . . . . . . . : Monday, February 21, 2022 1:44:38 PM
   Lease Expires . . . . . . . . . . : Friday, February 25, 2022 8:21:10 AM
   Default Gateway . . . . . . . . . : 10.0.1.1
   DHCP Server . . . . . . . . . . . : 10.0.1.1
   DHCPv6 IAID . . . . . . . . . . . : 389322742
   DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-24-1A-AA-C7-35-77-C6-14-0D-F4
   DNS Servers . . . . . . . . . . . : 2401:602:187f:2600::1
                                       10.0.1.1
   NetBIOS over Tcpip. . . . . . . . : Enabled

If your network has a DHCP server available, you can use the following commands to tell the DHCP server that your computer wants to refresh how it is connected to the network and to provide an IP Address to use, as well as other information such as the Gateway IP Address to connect to, netmask to utilize, and DNS servers to use for querying and translating domain names to IP addresses:

ipconfig /release
ipconfig /renew

NETSH

You can utilize the netsh command to manually set your IP address and DNS Servers. You can also use the netsh command to automatically query DHCP servers to set your IP Address and DNS servers automatically. Here is what the documentation shows regarding syntax for setting an IP address:

C:\Users\Brian>netsh interface ip set address /?

Usage: set address [name=]<string>
    [[source=]dhcp|static]
    [[address=]<IPv4 address>[/<integer>] [[mask=]<IPv4 mask>]
    [[gateway=]<IPv4 address>|none [gwmetric=]<integer>]
    [[type=]unicast|anycast]
    [[subinterface=]<string>]
    [[store=]active|persistent]

Parameters:

    Tag            Value
    name         - Interface name or index.
    source       - One of the following values:
                   dhcp: Enables DHCP for configuring IP addresses for
                         the specified interface.
                   static: Disables DHCP for configuring IP addresses for
                         the specified interface. This value must be
                         specified if an address or a gateway is being
                         configured.
    address      - IPv4 address to add or modify, optionally followed by
                   the subnet prefix length.
    mask         - The IP subnet mask for the specified IP address.
    gateway      - One of the following values:
                   <IPv4 address>: A specific default gateway for the
                                   static IP address you are setting.
                   none: No default gateways are set.  This is the default.
    gwmetric     - The metric for the default gateway. This field should
                   be set only if gateway is specified.
    type         - One of the following values:
                   unicast: Marks the address as a unicast address.
                            This is the default.
                   anycast: Marks the address as an anycast address.
    subinterface - LUID of the subinterface on which the default gateway
                   exists.  This parameter is only needed on interfaces
                   with multiple subinterfaces.
    store        - One of the following values:
                   active: Set only lasts until next boot.
                   persistent: Set is persistent.  This is the default.

Remarks: Used to enable or disable DHCP for IP address configuration.
         Also removes any previous static IP addresses and default gateways,
         and can add a new static IP address and default gateway.

Examples:

    set address name="Wired Ethernet Connection" source=dhcp
    set address "Wired Ethernet Connection" static 10.0.0.9 255.0.0.0 10.0.0.1 1

For example, the following command will configure your network interface card (NIC) to manually set your IP address:

netsh interface IP set address "Local Area Connection" static 192.168.0.125 255.255.255.0 192.168.0.1
netsh interface ip set dns "Local Area Connection" static 192.168.0.200

The first command above will set your IP address to 192.168.0.125 using the netmask of 255.255.255.0 and the gateway set to 192.168.0.1. The second command will set the DNS server to use to be 192.168.0.200.

If you instead want to configure your NIC to dynamically obtain its IP Address and DNS settings from the DHCP server then you would use the following:

netsh interface IP set address "Local Area Connection" dhcp
netsh interface ip set dns "Local Area Connection" dhcp

With the commands above this is assuming a couple of things.

The first is that the network adapter you are trying to change the IP address for is named Local Area Connection. This can be named differently, and the easiest way is to look in your Windows Control Panel for the correct name. Often times this name will have a number added to it such as Local Area Connection 2, or it may be completely different.

The second is the IP Address you are wanting to set is 192.168.0.125, but you can change this to whatever IP address you want to use.

Many times the default gateway and the DNS server will be the same IP, especially if you are using a router that has all of this built-in (most do).

Create a Batch Script

With a Windows batch script, you will usually create a file that ends with the extension .bat. A batch script can be a simple text file where you can store numerous commands to execute sequentially, such as the above ipconfig and netsh commands. You put everything in the file for how you want to run it, and then when you run the .bat file it will execute everything as you put it there.

With that said and this example batch script below, you will notice that it uses both the ipconfig and netsh programs as discussed above to do some of our work. Again the purpose of this script is to demonstrate how you can write a batch script to help you perform redundant tasks that you might often repeat. The script below is very basic, but may give you an idea of how you might start:

@ECHO off
cls

:start
ECHO.
ECHO Type the number below for which action you want to perform:
ECHO.
ECHO 1. Manually Set Static IP Address for Local Area Connection
ECHO 2. Manually Set Static IP Address for Local Area Connection 2
ECHO 3. Manually Set Static IP Address for Local Area Connection 3
ECHO 4. Obtain an IP address automatically for Local Area Connection
ECHO 5. Obtain an IP address automatically for Local Area Connection 2
ECHO 6. Obtain an IP address automatically for Local Area Connection 3
ECHO 7. Exit
ECHO.
set choice=
set /p choice=What would you like to do?: 
if not '%choice%'=='' set choice=%choice:~0,1%
if '%choice%'=='1' goto con1
if '%choice%'=='2' goto con2
if '%choice%'=='3' goto con3
if '%choice%'=='4' goto autosearch1
if '%choice%'=='5' goto autosearch2
if '%choice%'=='6' goto autosearch3
if '%choice%'=='7' goto end
ECHO "%choice%" is not valid, try again
ECHO.
goto start

:con1
ECHO Manually setting static IP address for Local Area Connection
netsh interface ip set address "Local Area Connection" static 192.168.0.25 255.255.255.0 192.168.0.1 1
goto end

:con2
ECHO Manually setting static IP address for Local Area Connection 2
netsh interface ip set address "Local Area Connection 2" static 192.168.0.25 255.255.255.0 192.168.0.1 1
goto end

:con3
ECHO Manually setting static IP address for Local Area Connection 3
netsh interface ip set address "Local Area Connection 3" static 192.168.0.25 255.255.255.0 192.168.0.1 1
goto end

:autosearch1
ECHO Obtaining IP Address automatically for Local Area Connection
ipconfig /renew "Local Area Connection"
goto end

:autosearch2
ECHO Obtaining IP Address automatically for Local Area Connection 2
ipconfig /renew "Local Area Connection 2"
goto end

:autosearch3
ECHO Obtaining IP Address automatically for Local Area Connection 3
ipconfig /renew "Local Area Connection 3"
goto end

:bye
ECHO BYE
goto end

:end

Once you create the above .bat script, then you would double-click on the file/program to run it and you will see something like this from a command console:

Set IP Address Batch Script

add a comment
0