How to configure Windows TCP/IP settings from the Command Prompt (CLI)

On March 17, 2011, in How-to, Scripting, by Cubert aka (Cube Dweller)

Window’s Netsh.exe command-line scripting utility  allows you to display or modify the network configurations of any computer that is currently running a resent flavor of Windows. Netsh.exe also provides a scripting feature that allows you to run a group of commands in batch mode against a computer both locally and remotely.  You can also use Netsh.exe to save a configuration script in a text file for archival purposes or to help you configure network information when changing networks.

Here are your commands and what they can do.

View your TCP/IP settings.

netsh interface ip show config

 

Configure your computer’s IP address and other TCP/IP related settings.

This command configures the interface named Local Area Connection with the static IP address 192.168.0.101, the subnet mask of 255.255.255.0, and a default gateway of 192.168.0.1:

netsh interface ip set address name="Local Area Connection" static 192.168.0.101 255.255.255.0 192.168.0.1 1

Configure your NIC to automatically obtain an IP address from a DHCP server

netsh interface ip set address "Local Area Connection" dhcp

To configure DNS and WINS addresses from the Command Prompt

netsh interface ip set dns "Local Area Connection" static 192.168.0.101
netsh interface ip set wins "Local Area Connection" static 192.168.0.101

To configure your NIC to dynamically obtain it’s DNS settings

netsh interface ip set dns "Local Area Connection" dhcp

Now let’s say you have a laptop and it travels between 2 separate networks and you need to maintain separate static configurations for each network. There is a simple way to flip back and forth between networks by creating a simple batch file. Here is how it’s done.

First we export your current IP settings to a text file:

netsh -c interface dump > c:\network1.txt

Next we change the settings manually on the interface to the second network and then export it as well:

netsh -c interface dump > c:\network2.txt

Now we have 2 text files, each file represents a network location. Whenever you need to quickly change your IP settings to switch between networks you can run the following commands

netsh -f c:\network1.txt
or
netsh -f c:\network2.txt

Now to make a batch file to execute the command just create a file called network1.bat and network2.bat and add the perspective line to that file. You can also use the global EXEC switch instead of  “-f ”  switch in the batch file with the same results.

Here are all the options available to netsh.exe

Usage: netsh [-a AliasFile] [-c Context] [-r RemoteMachine] [-u [DomainName\]UserName] [-p Password| *]
    

Commands available:

?              – Displays a list of commands.
add            – Adds a configuration entry to a list of entries.
advfirewall    – Changes to the `netsh advfirewall’ context.
branchcache    – Changes to the `netsh branchcache’ context.
bridge         – Changes to the `netsh bridge’ context.
delete         – Deletes a configuration entry from a list of entries.
dhcp           – Changes to the `netsh dhcp’ context.
dhcpclient     – Changes to the `netsh dhcpclient’ context.
dnsclient      – Changes to the `netsh dnsclient’ context.
dump           – Displays a configuration script.
exec           – Runs a script file.
firewall       – Changes to the `netsh firewall’ context.
help           – Displays a list of commands.
http           – Changes to the `netsh http’ context.
interface      – Changes to the `netsh interface’ context.
ipsec          – Changes to the `netsh ipsec’ context.
lan            – Changes to the `netsh lan’ context.
mbn            – Changes to the `netsh mbn’ context.
namespace      – Changes to the `netsh namespace’ context.
nap            – Changes to the `netsh nap’ context.
netio          – Changes to the `netsh netio’ context.
p2p            – Changes to the `netsh p2p’ context.
ras            – Changes to the `netsh ras’ context.
rpc            – Changes to the `netsh rpc’ context.
set            – Updates configuration settings.
show           – Displays information.
trace          – Changes to the `netsh trace’ context.
wcn            – Changes to the `netsh wcn’ context.
wfp            – Changes to the `netsh wfp’ context.
winhttp        – Changes to the `netsh winhttp’ context.
winsock        – Changes to the `netsh winsock’ context.
wlan           – Changes to the `netsh wlan’ context.

 

Enjoy

Cubert

Tagged with:
 

1 Response » to “How to configure Windows TCP/IP settings from the Command Prompt (CLI)”

  1. Banigo says:

    Thank you so much. This was very helpful.

Leave a Reply