Sort Network Adapter via PowerShell

Sort Windows Network Adapter by PCI Slot via PowerShell

If you work with Windows, Windows Server or Hyper-V you know that before Windows Server 2012 Windows named the network adapters randomly. This was a huge deal if you were trying to automate deployment of servers with multiple network adapters. And of course Hyper-V Servers normally have multiple network adapters. In Windows Server 2012 Microsoft had some different ways how this was fixed. First there is CDN (Consistent Device Naming) which allows hardware vendors to integrate the names so the OS can pick them up and the second one being the possibility of Hyper-V Converged Fabric which is basically making our lives easier by having less network adapters.

Well a lot of vendors have not integrated CDN or you have some old servers without CDN support. Back in May 2012 before the release of Windows Server 2012 I wrote a little Windows PowerShell script to sort network adapters in Windows Server 2008 R2 and Hyper-V Server 2008 R2 by using WMI (Configure Hyper-V Host Network Adapters Like A Boss). Now for a Cisco UCS project I rewrote some parts of the script to use Windows PowerShell in for Windows Server 2012, Windows Server 2012 R2 and Hyper-V.

First lets have a look how you can get the PCI slot information for network adapters, luckily there is now a PowerShell cmdlet for this.

 
Get-NetAdapterHardwareInfo

Now lets see how you can sort network adapters via Windows PowerShell.

 
Get-NetAdapterHardwareInfo | Sort-Object Bus,Function

This will get you a output like this:

Sort Network Adapter via PowerShell

Lets do a little loop to automatically name them:

$prefix = "NIC"
$netAdapters = Get-NetAdapterHardwareInfo | Sort-Object Bus,Function
$i = 0
 
foreach ($netAdapter in $netAdapters){
 
$interface = $netadapter | Get-NetAdapter
$old = $interface.Name
$newName = $prefix + $i
$interface | Rename-NetAdapter -NewName $newName
$i++
Write-Host "Rename" $old "to:" $newName
 
}

So this names all the network adapters to NIC1, NIC2, NIC3,…

So lets do a PowerShell function for this:

# ---------------------------------------------------------------------------------------------- #
# Powershell Sort-NetworkAdapter $Rev: 748 $
# (c) 2014 Thomas Maurer. All rights reserved.
# created by Thomas Maurer
# www.thomasmaurer.ch
# last Update by $Author: tmaurer $ on $Date: 2014-01-04 14:07:36 +0100 $
# ---------------------------------------------------------------------------------------------- #
 
function Sort-NetworkAdapter {
<# .SYNOPSIS This sorts and renames network adpaters by PCI slot .DESCRIPTION This sorts and renames network adapters sorted by PCI slot .EXAMPLE Sort-NetworkAdapter -prefix vnic -StartingNumber 0 This renames als NICs to vnic0, vnic1, vnic2,... .EXAMPLE Sort-NetworkAdapter -prefix nic -StartingNumber 1 This renames als NICs to nic1, nic2, nic3,... .PARAMETER prefix The Prefix of the network adapter name .PARAMETER StartingNumber The Number of the first network adapter #>
[CmdletBinding(SupportsShouldProcess=$True,ConfirmImpact='Low')]
param
(
[Parameter(Mandatory=$True,
ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True,
HelpMessage='Which prefix you want to use?')]
[ValidateLength(1,20)]
[string]$prefix,
 
[Parameter(Mandatory=$False,
ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True,
HelpMessage='Which Starting Number you want to use?')]
[int]$startingNumber = 1
)
 
begin {
write-verbose "Get netadpaters and sort them"
$netAdapters = Get-NetAdapterHardwareInfo | Sort-Object Bus,Function
}
 
process {
 
write-verbose "Rename netadapters"
 
foreach ($netAdapter in $netAdapters){
 
$interface = $netadapter | Get-NetAdapter
$old = $interface.Name
$newName = $prefix + $startingNumber
#$interface | Rename-NetAdapter -NewName $newName
$startingNumber++
Write-Host "Rename" $old "to:" $newName
 
}
}
}

Now you can run this by using Sort-NetworkAdapter for exmaple:

Sort-NetworkAdapter -prefix NIC

or

Sort-NetworkAdapter -prefix NIC -StartingNumber 0

You can also get this script from the Microsoft Technet Gallery or Script Center.