Use PowerShell to Configure the NIC on Windows Server 2012

Doctor Scripto

Summary: Guest blogger and Microsoft MVP Jan Egil Ring talks about using Windows PowerShell 3.0 to automatically configure the NIC on Windows Server 2012. Microsoft Scripting Guy, Ed Wilson, is here. Yesterday, The Scripting Wife and I traveled from Stockholm, Sweden, to Oslo, Norway, and had dinner with MVP Jan Egil Ring. Today, I will speak at the User group here in Oslo. Therefore, today seems like a good day to have Jan as our guest blogger. Read more about Jan and see his previous post. Take it away, Jan. With the release of Windows Server 2012, Microsoft provided native Windows PowerShell support for NIC configuration tasks. A complete overview of the Windows PowerShell Cmdlets for Networking is available in the TechNet Library. A typical usage scenario is configuring TCP/IP settings, for which we can leverage the Net TCP/IP Cmdlets that are available in the NetAdapter module.

# Import the NetAdapter module. This step is provided for clarity. It is not needed due to the new module autoloading feature in Windows PowerShell 3.0

Import-Module NetAdapter  # Retrieve the network adapter that you want to configure.

$netadapter = Get-NetAdapter -Name Ethernet  # Disable DHCP.

$netadapter | Set-NetIPInterface -DHCP Disabled  # Configure the IP address and default gateway.

$netadapter | New-NetIPAddress -AddressFamily IPv4 -IPAddress -PrefixLength 24 -Type Unicast -DefaultGateway  # Configure the DNS client server IP addresses.

Set-DnsClientServerAddress -InterfaceAlias Ethernet -ServerAddresses For your reference, the equaling commands in network shell (netsh.exe) are the following:

netsh interface ip set address name=”Ethernet” static 1

netsh interface ip set dns “Ethernet” static A more advanced usage scenario is configuring TCP/IP settings for computers with multiple NICs. One example is Hyper-V cluster nodes, which may have the following network adapters:

  • Management – Used for remote management, domain traffic, management agents, and so on.
  • Heartbeat – Dedicated NIC for heartbeat in failover clustering.
  • CSV – Dedicated NIC for Cluster Shared Volume network traffic.
  • Live Migration – Dedicated NIC for live-migrating virtual machines between Hyper-V hosts.
  • Virtual switch – Dedicated NIC for an external virtual network switch in Hyper-V, typically connected to a trunked network switch port.

The number of NICs may vary based on the number of virtual switches and usage of NIC teaming. In our example scenario, we will configure a Hyper-V host with 5 NICs. Configuring TCP/IP settings and alias names for NICs in such a scenario might be a time-consuming task because you will have to figure out which NIC is connected to which VLAN/switch port. One way to ease this scenario is to prepare a CSV-file containing the necessary configuration by using, for example, Microsoft Excel. The CSV-file may contain configuration info for all the NICs for one or more servers. This is a sample CSV-file:






“HPV10001″,”Hyper-V_LiveMigration”,”00-25-B5-01-00-FF”,”false”,”IPv4″,”″,”24″,, You can then leverage the cmdlets in the NetAdapter module to automatically configure TCP/IP settings on the Hyper-V host:

# Importing the CSV-file containing the NIC configuration.

$NICs = Import-Csv “c:tempnic-config.csv” | Where-Object {$_.computername -eq $env:COMPUTERNAME}

foreach ($NIC in $NICs) {

$NetAdapter = Get-NetAdapter | Where-Object {$_.MacAddress -eq $NIC.MAC}

if ($NetAdapter) {

Write-Verbose “Found NIC $($NIC.NIC)”  # Retrieving the network adapter you want to configure.

$NetAdapter = $NetAdapter | Rename-NetAdapter -NewName $NIC.NIC -PassThru  # Configuring a static IP address for the NIC, if DHCP is set to false in the CSV-file

if ($NIC.DHCP -eq ‘false’) {

Write-Verbose “Configuring TCP/IP settings for NIC $($NIC.NIC)”

$NetAdapter = $NetAdapter | Set-NetIPInterface -DHCP Disabled -PassThru  # Initializing empty hash table for storing NIC configuration.

$NICAttributes = @{}  # Adding configuration properties to hash table.

if ($NIC.AddressFamily) {



if ($NIC.IPAddress) {



if ($NIC.PrefixLength) {



if ($NIC.Type) {



if ($NIC.DefaultGateway) {


}  # Configuring IP address settings by using splatting.

$NetAdapter | New-NetIPAddress @NICAttributes  # Configuring DNS client server address, if defined in the CSV-file.

if ($NIC.DnsServerAddresses) {

Set-DnsClientServerAddress -InterfaceAlias $($NIC.NIC) -ServerAddresses $NIC.DnsServerAddresses





Note   The purpose of this sample is to provide example usage. In a production environment, you would typically want to add functionality such as error handling and logging. While this automates the configuration phase, you still need to manually gather the MAC address for each NIC from the hardware layer. If the hardware vendor supports some kind of automation, you could also gather that information automatically. One such example is Cisco`s Unified Computing System (UCS), which provides a Windows PowerShell module for automating every aspect of the physical server hardware. For more information, see Getting started with Cisco UCS PowerShell Toolkit on my blog. The following is a sample script that will generate a CSV-file containing the server name, NIC name, and MAC address for all Hyper-V servers in a Cisco UCS system: # Importing the Cisco UCS PowerShell module (UCS PowerTool)

Import-Module CiscoUcsPS

Connect-Ucs -Name -Credential (Get-Credential)

$output = @()

$path = “c:tempnic-config.csv”

$serverprofiles = Get-UcsServiceProfile | Where-Object {$ -like “*hpv*”}

foreach ($server in $serverprofiles) {

$VNICs = Get-UcsVnic -ServiceProfile $($

foreach ($VNIC in $VNICs) {

Note   A trunked interface will contain several VnicInterface objects. # Since all of them will have the same IP Address, we can retrieve the first one.

$VnicInterface = $VNIC | Get-UcsVnicInterface | Select-Object -First 1

$output += New-Object -TypeName pscustomobject -Property @{

Computername = $($

NIC = $VnicInterface.Name

MACAddress = $VnicInterface.Addr

VLAN = $VnicInterface.Vnet




$output | Select-Object Computername,NIC,VLAN,MAC,DHCP,AddressFamily,IPAddress,PrefixLength,Type,DefaultGateway,DnsServerAddresses | Export-Csv -Path $path -NoTypeInformation

Note  I also added some properties that are not available on the custom object. This is used to create a CSV-file containing all the properties needed for a template you can edit later for use with the NIC configuration script.


In this post, we have looked at basic usage of one of the networking modules available in Windows Server 2012, as well as a usage scenario for automation together with a third-party Windows PowerShell module. Some of the advantages over previous command-line utilities like netsh.exe is the discoverability and the object model that Windows PowerShell provides. For example, we can use cmdlets like Get-Command, Show-Command, Get-Help, and Get-Member to explore what is available, as well as the generic *-Object cmdlets to manipulate the data in the way we want.

Using PowerShell for NIC Configuration Tasks Use PowerShell 3.0 to Work with Network Adapters Hyper-V Cmdlets in Windows PowerShell Windows PowerShell Support for Windows Server 2012 ~Jan Thank you, Jan. This is an excellent article. Awesome job! I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy


Discussion is closed.

Feedback usabilla icon