Work with Network Adapters in command line (Part 2)

In my last article (Work with Network Adapters in command line (Part 1)), we saw how to work with the IP configuration using Powershell. For multiple reasons, I prefer this way because it’s more versatile, however, there are many cases where you might need to use an older method to achieve the same result. Let’s introduce you netsh.

Netsh is a command-line utility that allows you to work with the TCP/IP configuration of a running device. The main advantage is that it has been introduced with Windows 2000 and is still included in the latest Windows version. That means that it will work on almost every device you encounter. It also means that it’s old and doesn’t allow as much possibility that its Powershell counterpart (NetTCPIP).

Get the current IP configuration

Ok, so we all know Ipconfig right? Let’s make it more fun and use netsh instead.

netsh interface ip show config

So here, we see many pieces of information. We see the IP, Subnet, Gateway as well as DNS. We also see the Interface name, in our case, “Ethernet“. Let’s remember this because we will need it later.

Now let’s have some fun!

Work with the Network Adapter IP Configuration

Ok, first let’s set a static IP address for our adapter named “Ethernet

netsh interface ip set address name="Ethernet" static 192.168.1.111 255.255.255.0 192.168.1.1

So as you can see, now “DHCP enabled” is set to “No”. That means that we have set our static IP address. Of course, you might already see what’s wrong here. That’s right, no DNS is set. This is because you need to configure your DNS manually every time you set a static IP address. This is true even when you do this with the GUI. Let’s configure our DNS then!

netsh interface ip set dns "Ethernet" static 8.8.8.8

And if you want to set a secondary DNS server, the following command will do the trick!

netsh interface ip add dnsservers name="Ethernet" address="1.1.1.1" index=2

Ok, now let’s say we want to disable the network adapter. We need to run the following command :

netsh interface set interface name="Ethernet" admin=disabled

So as you can see, the interface “Ethernet” is not showing anymore. One good way to see all interfaces, including the disabled one is the following command :

netsh interface show interface

Ok, so now, let’s enable it back and run our last command again

netsh interface set interface name="Ethernet" admin=enabled
netsh interface show interface

As you see in the last screenshot, the interface is now shown as “Connected” with the command netsh interface show interface and the interface is back when we run netsh interface IP show config.

And how do we set the IP back to DHCP? It’s easy, just run the following command!

netsh interface ip set address name="Ethernet" source=dhcp

And like we did earlier when setting the static IP/DNS, we need to set the DNS to DHCP separately using the following command:

netsh interface ip set dnsservers name="Ethernet" source=dhcp

And we’re back to our interface dynamically configured with DHCP.

So you might be wondering why not use netsh all the time instead of Powershell? That’s a fair question and in many cases, there isn’t much advantage in using Powershell for this. One of the main reasons for me to use Powershell is for all the automation we can do. Again, go see part 1 to see an example of what’s possible with Powershell.

Another reason is that netsh didn’t really evolve since Windows 2000. They even removed some functionality to netsh when Vista was released.

Also, Microsoft is pushing Powershell a lot which makes me wonder how much longer the old command-line utility will be included before Microsoft decide that it’s time to move on to Powershell. I mean we can do almost anything using Powershell nowadays.

There are still, however, many occasions where Powershell can’t do the job. Devices where Powershell is not up to date or older Windows versions. In those cases, I find netsh quite useful.