0

I need to rename NIC cards based on many consistent PnPDeviceID's.

So far the code below was the closest I was able to get. But it only displays the NetConnectionID and PnPDeviceID, but can't use it to manipulate the NetConnectionID.

$interfaces = Get-WmiObject Win32_NetworkAdapter
$interfaces | foreach {
$name = $_ | Select-Object -ExpandProperty NetConnectionID
if ($name) {
$id = $_.GetRelated("Win32_PnPEntity") | Select-Object -ExpandProperty DeviceID
Write-Output "$name - $id"
}
}

I hope this can be a simple script that will successfully rename the NIC card.

  • As an aside: The CIM cmdlets (e.g., `Get-CimInstance`) superseded the WMI cmdlets (e.g., `Get-WmiObject`) in PowerShell v3 (released in September 2012). Therefore, the WMI cmdlets should be avoided, not least because PowerShell _Core_, where all future effort will go, doesn't even _have_ them anymore. For more information, see [this answer](https://stackoverflow.com/a/54508009/45375). – mklement0 Nov 10 '19 at 23:01

1 Answers1

0

I would use Get-NetAdapter and Rename-NetAdapter instead mucking around with Get-WmiObject.

https://learn.microsoft.com/en-us/powershell/module/netadapter/get-netadapter?view=win10-ps

https://learn.microsoft.com/en-us/powershell/module/netadapter/rename-netadapter?view=win10-ps

The Get-NetAdapter cmdlet gets the basic network adapter properties. By default only visible adapters are returned.

The Rename-NetAdapter cmdlet renames a network adapter. Only the name, or interface alias can be changed.

#Create your array of adapters
$netAdapters = Get-NetAdapter 

#loop through all adapters
foreach($adapter in $netAdapters)
{

    #Set the requirements that need to be met in order to change the adapter name
    $PnPIDs2Change = "PCI\VEN_8086&DEV_1539&SUBSYS_15391849&REV_03\7085C2FFFFBA0CFC00","PCI\VEN_8086&DEV_1539&SUBSYS_15391849&REV_03\7085C2FFFFBA0CFC01","PCI\VEN_8086&DEV_1539&SUBSYS_15391849&REV_03\7085C2FFFFBA0CFC02","PCI\VEN_8086&DEV_1539&SUBSYS_15391849&REV_03\7085C2FFFFBA0CFC03","PCI\VEN_8086&DEV_1539&SUBSYS_15391849&REV_03\7085C2FFFFBA0CFC04"

    #Since you have "many consistent PnPDeviceID's" you need to loop through each of those PnPIDs and check them against the adapter in question
    foreach($ID in $PnPIDs2Change)
    {

        #If adapter meets the requirements, change the name 
        if($adapter.PnPDeviceID -eq "$ID")
        {
            #Take note of the old name and ID
            $oldName = $adapter.Name
            $adapterPnPID = $adapter.PnPDeviceID
            #Uncomment the 2 lines below in order to actually rename the adapter(set $newName to what you actually want it set to)
            #$newName = "Public - VLAN 1"
            #Rename-NetAdapter -Name $adapter.Name -NewName $newName
            Write-Output "$oldName - $adapterPnPID"
        }
    }
}
Scab
  • 26
  • 4