Renaming the network interface name via command line

Kiran6699 picture Kiran6699 · Jan 14, 2013 · Viewed 11.8k times · Source

I want to rename the network interface name to a default name, for example "Ethernet", via dos.

I know netsh interface set interface name=”Local Area Connection” newname=”Ethernet” is the way to do it.

I am running a script and it will not know the name of the interace, correct? So, I need it to rename it to a default before I set the IP address for the interface.

How can I do this without knowing the name of the interface?

Is there a way to get the name of the interface somehow and then change it?

Answer

papo picture papo · Feb 14, 2016

netsh (a tool MS tried to wean us off and gave up)

To get the WLAN interface names: netsh wlan show interfaces
      help states: show interfaces - Shows a list of the wireless LAN interfaces on the system.
To get LAN interface names: netsh lan show interfaces
      help states: show interfaces - Shows a list of the current wired interfaces on the system.
To get names of all interfaces: netsh interface show interface
      help states: show interfaces - Displays interfaces.

The command for LAN does need Wired AutoConfig service, which is usually not started.

This Batch script would start the service, get (the last) LAN interfaces name, change it to a new name, stop the Wired AutoConfig service again.

sc.exe start dot3svc

for /f "tokens=1* delims=: " %%a in ('netsh lan show interfaces') do if %%a == Name set activeAdapter=%%b
echo %activeAdapter%
netsh interface set interface name="%activeAdapter%" newname="Ethernet"

sc.exe stop dot3svc

Limitations:

  • It assumes there is only one wired interface
  • It will stop Wired AutoConfig service whether it was running or not at the start

For Wifi interfaces, change in above script 'lan' to 'wlan' and remove both sc.exe service start/stop


PowerShell

To get better control of what interface is renamed PowerShell will be an easier choice.

This will probably work in most cases:
Get-NetAdapter | Where-Object { $_.HardwareInterface -eq $True -and $_.MediaType -eq "802.3" } | Rename-NetAdapter -NewName "Ethernet"

HardwareInterface will eliminate virtual interfaces, e.g. VMWare
MediaType 802.3 will only show "wired" interfaces and not Wifi, Broadband or others.

In a script file, use the above version of the command, shorthand version would be:
Get-NetAdapter | ? HardwareInterface | ? MediaType -eq "802.3" | Rename-NetAdapter "Ethernet"

There are more options how to select the desired interface. Check all parameters by which it can be selected:
(Get-NetAdapter)[0] | Format-List -Property * -Force

e.g.
List all made by Realtek (Realtek vendor 10ec, Intel: 8086):
Get-NetAdapter | ? ComponentID -like "PCI\VEN_10EC*"

Not virtual:
Get-NetAdapter | ? Virtual -eq $false

Connector Present:
Get-NetAdapter | ? ConnectorPresent

Then there is WMI object
Get-WmiObject -Class Win32_NetworkAdapterConfiguration


Registry

In a case that you changed a Network adapter card and a new one is using a name: Ethernet 2 or similar and you want to rename it back to "Ethernet", there will be an error saying:

"You were not connected because a duplicate name exists on the network. If joining a domain, go to System in Control Panel to change the computer name and try again. If joining a workgroup, choose another workgroup name."

Which is of course erroneous error message about renaming a computer.
PowerShell would say correctly: An attempt was made to create an object and the object name already existed.

But going to Control Panel\Network and Internet\Network Connections and trying to rename the interface there would not help either.

It seems the only option for such case is to find corresponding key in these registry paths:

HKEY_LOCAL_MACHINE\SYSTEM\Setup\Upgrade\NetworkDriverBackup\Control\Network\{4d36e972-e325-11ce-bfc1-08002be10318}\
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\
HKEY_LOCAL_MACHINE\SYSTEM\Setup\Upgrade\NetworkDriverBackup\Control\NetworkSetup2\Interfaces\

and remove the key for the old interface (not the whole paths as written above!), then restart.
Renaming by cmd batch, PS or in Control Panel should now work.