Powershell script to change NIC label based on location

By | April 25, 2011

Here’s a little WMI script I threw together the other day to help someone out.  The requirement was to set the display name (label) of a network adapter based on its hardware location.

Let’s say we have a NIC with the display name “Local Area Connection” and you want to change it to “MyNIC”, but only if it has a specific hardware location (e.g. PCI bus 3, device 0, function 0).  Of course, you can do this manually by nipping into the network connection properties (ncpa.cpl), but what if you have tens or hundreds of servers that you want to do this for?

nic_details_before.jpg

nic_configure.jpg

######################################################### 
# 
# Name: SetNetConnectionID.ps1 
# Author: Tony Murray 
# Version: 1.0 
# Date: 20/04/2011 
# Comment: PowerShell script to 
# change device display name for network adapter based 
# on hardware location 
# 
######################################################### 
$newname = "MyNIC" 
$location = "PCI bus 3, device 0, function 0" 
$drvinfo = (Get-WMIObject Win32_PnPSignedDriver `
| ?{$_.Location -eq $location}).DeviceID 
$nic = Get-WMIObject Win32_NetworkAdapter `
| ?{$_.PNPDeviceID -eq $drvinfo} 
$oldname = $nic.NetConnectionID 
write-host "Network Adapter name before change: " $oldname 
$nic.NetConnectionID = $newname 
$nic.Put()

After running the script, you should see that the display name has changed.

nic_details_after.jpg

My script only changes the NIC display name for the computer on which it is running, but you could easily change this to run against multiple machines.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.