Donnerstag, 23. Februar 2012

Rename your network adapter using Powershell and netsh

Often I have the problem that my network adapters are labeled wrong. So I want to rename them. The easiest way to rename your adapter is to use netsh. But if you want to you netsh you are limited to one adapter. What would you do if you want to do this dynamicly and for more than one adapter, doesn't matter how much NIC are installed. Yes, use powershell!!!!

In this example I will show you how to get the name of a network adapter with a specific ip address.
First you have to get all of your adapters by using WMI. You can find all installed adpaters in the win32_networkadapter and win32_networkadapterconfiguration classes.
We start to get all instances of the win32_networkadapterconfiguration:
Type gwmi win32_networkadapterconfiguration

you will get more than 1 result, also if you only have one network card.

The network interface I want to change is "Atheros AR5B97 Wireless Network Adapter".
What we need is the index of your network interface, so we have pipe our output into a where-object:
Type  gwmi win32_networkadapterconfiguration | ? {$_.ipaddress -like "*192.168*"}
Instead of my *192.168* you can type your own ip address.

we need this later again, so we put this command into a variable. and let us show the index:

If you have more than one interface with ip like "192.168*" this will fail, then you have to use a foreach loop.

The rest is easy now. You have to use the index of your interface to get the networkconnectionID from the win32_networkadapter class:
 $networkadapterID = gwmi win32_networkadapter | ? { $_.index -eq ($networkadapter).index}

Your Output will be the same as it is in your control center:
To use netsh you have to know how it is labeled.
The netsh command to rename your network inerface is:
netsh interface set interface "<your interface name>" newname="NewName"

How to include this into powershell?
This is also very simple:
First you have to put this command into a variable:
$input = @"
interface set interface "$oldinterfacename" newname="NewName"
Second and last step is to pipe this input into netsh:
Type: $input | netsh
 You are finished now.

To give you one more example, I have used this commands for more than one interface.

$adapter = gwmi win32_networkadapterconfiguration | ? { $_.IPaddress -like "*192.168*"}
$i = 1
foreach($element in $adapter){
$adapterindex = $element.index
$newname = "Adapter"+$i
$adapterID = gwmi win32_networkadapter | ?{$_.index -eq $adapterindex}
$adapter = ($adapterID).NetConnectionID
    $input = @"
interface set interface "$adapter" newname="$newname"
$input | netsh

I hope this will help you, and please forgive my bad teaching skills. this is my first time :D 

1 Kommentar:

  1. This is a very informative post. I will keep this in mind for further research. I think you should visit this site for further topics to dicuss. I hope you like it.
    UK Euro AV