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.
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:
Type:
$networkadapterID = gwmi win32_networkadapter | ? { $_.index -eq ($networkadapter).index}
($networkadapterID).netconnectionID
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:
Type:
$input = @"
interface set interface "$oldinterfacename" newname="NewName"
"@Second and last step is to pipe this input into netsh:
Type: $input | netshYou 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"
exit
"@
$input
$input | netsh
$i++
}
I hope this will help you, and please forgive my bad teaching skills. this is my first time :D