Renaming the network interface name via command line [closed]
Asked Answered
P

2

5

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?

Pleurisy answered 14/1, 2013 at 11:39 Comment(0)
G
9

netsh

(netsh tool might be deprecated, see at the bottom)

get all WLAN interfaces names: netsh wlan show interfaces
      "show interfaces - Shows a list of the wireless LAN interfaces on the system."

get LAN interface names: netsh lan show interfaces
      "show interfaces - Shows a list of the current wired interfaces on the system."

get names of all interfaces: netsh interface show interface
      "show interfaces - Displays interfaces."
      Needs "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 of above script:

  • 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

In PowerShell it's easier to select the correct interface.

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

Explanation of the above command:
HardwareInterface to skip virtual interfaces, e.g. VMWare
MediaType == 802.3 to only show "wired" interfaces and not Wifi, Broadband or others.

A short version of the above command. (In a script file, use the long version above)
Get-NetAdapter | ? HardwareInterface | ? MediaType -eq "802.3" | Rename-NetAdapter "Ethernet"

-

If the above command can't safely select the one desired interface, use this command to list all interfaces with their detailed parameters.
Get-NetAdapter | Format-List -Property * -Force
or list them one by one (replace "0" with "1", "2", ...):
(Get-NetAdapter)[0] | Format-List -Property * -Force
Use the output to make the command more specific.

e.g.
Interfaces 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

-

Even more options are available using the WMI object:
Get-WmiObject -Class Win32_NetworkAdapterConfiguration


Registry

Sometimes to rename the interface using either netsh or PS does not work and the only way is to edit the registry.
e.g. In a case where you changed a Network adapter card and the new one is using a name: Ethernet 2 or similar and you want to rename it back to "Ethernet".
This is because the old interface is still in the registry, but not visible by those tools.

Using netsh to rename the interface will in such case show this weird nonsensical error:

"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."

Using PowerShell would show more suitable error:

An attempt was made to create an object and the object name already existed.

Going to Control Panel\Network and Internet\Network Connections and trying to rename the interface there would not work either.

It seems the only way in such case is to find the Key corresponding to the old, removed interface 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\

remove the key for the old interface (not the whole path as written above!),
restart,
then rename using netsh, PowerShell or manually in the Control Panel. It should work now.


Is netsh deprecated?

Around (or at least in) 2013, the tool started to show a warning:

... Microsoft might remove the Netsh functionality for ...

Now ten years later, it's still working. Tough if you're writing a script and not just one time fix, it might be more prudent to use the PowerShell way.

Grantinaid answered 14/2, 2016 at 18:41 Comment(1)
Update: 2023 and netsh is still part of a standard Windows installation.Lindsley
Y
0

You can do quite a lot with Powershell, which depending on how new your server is, may be installed. Google "Get-WmiObject -Class Win32_NetworkAdapterConfiguration" to see more info.

Yarkand answered 14/1, 2013 at 12:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.