NetSH scripting

I have been playing with two windows cli’s lately the first is the NetSH command which ships with everything from Windows 2000 on up. Will write up a post once I am more comfortable with wmic.

The main purpose of the tool is for network administration using the command line. The first thing I thought of was “YAY a way to change my IP for where I am” and that was how the batch file started. Yes I do know there are better scripting languages around, but I went with something that should work on whatever Windows 2000+ operating system it gets dumped in.

In my quest to manage my NIC’s from a script I came across another excellent Windows administration tool DEVCON which is what I used to Dis/Enable the network cards.

Dis/Enabling Network Cards

Controlling the state of the network cards was really easy once I found out about DevCon. I just extracted the files in the DevCon zip file into a place in my Windows path statement, then you run the command with the Device ID like below. You will need to replace the ID (in italics) with the one for you network card. You can find this by opening Device Manager, open the properties of the device you want to control. The Device ID can be found under the Details tab. If anyone know how to get the Device ID using DevCon I am sure it will be appreciated in the comments.

devcon disable PCIVEN_168C&DEV_0024&SUBSYS_0087106B&REV_014&139D1158&0&00E4

devcon enable PCIVEN_168C&DEV_0024&SUBSYS_0087106B&REV_014&139D1158&0&00E4

Getting some IP information

To show the ip configuration for all active network devices use

NETSH INTERFACE IP SHOW CONFIG

To only show a network you can add “Local Area Connection” or “Wireless Network Connection” to the end of the command.

Set the IP on a Network

NETSH INTERFACE IP SET ADDRESS “Local Area Connection” STATIC 192.168.0.36 255.255.255.0

NETSH INTERFACE IP SET ADDRESS “Wireless Network Connection” STATIC 192.168.0.36 255.255.255.0

This command is fairly self explanatory, replace the IP with what ever is applicable. In my script I have an item that allows you to enter your own address which makes changing to a unusual (read seldom used) IP really easy. You can always add more items if you need them.

Of course you can also set the interface to use DHCP

NETSH INTERFACE IP SET ADDRESS “Local Area Connection” DHCP

NETSH INTERFACE IP SET ADDRESS “Wireless Network Connection” DHCP

To add DNS entries you can use

netsh interface ip set dns name=”Local Area Connection” 192.168.1.230

If you need more DNS entries you would use the following syntax

netsh interface ip add dns name=”Local Area Connection” 192.168.1.231

Links – Further Reading

Files

Creative Commons License
netsh scripting by Kim Alan White is licensed under a Creative Commons Attribution-Share Alike 2.5 South Africa License

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.