Use PowerShell to Configure the NIC on Windows Server 2012

Use PowerShell to Configure the NIC on Windows Server 2012

  • Comments 11
  • Likes

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:







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:\temp\nic-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:\temp\nic-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.

Related resources

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


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

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • great article and script.

  • This can be done via WMI object in v2:

    #Setup NICS

    $NIC = Get-WmiObject Win32_NetworkAdapterConfiguration -filter "MACAddress='$($xmlConfigInfo.VMS.VM[0].MACAddress1)'"

    $NIC.EnableStatic($xmlConfigInfo.VMS.VM[0].IP1, "")


  • great article and script.

    Looking forward to the session with Ed Wilson at MTUG this evening.

  • Thanks for sharing this, very useful stuff!

  • I helped someone in Technet Forums last week about using powershell to manage NIC Teaming. It would be much easier if I knew this article :D

    Great article!

  • Useful article.

  • Hi,

    I have network name differences between Netwok Connections applet and Failover Cluster Manager.

    I'm trying to rename a cluster net interface using Get-ClusterNetworkInterface. So I run $n = Get-ClusterNetworkInterface "oldname", then $ = "newname". I'm getting  a "Exception setting "Name": "Operation is not valid due to the current state of the object." error.

    But, Get-ClusterNetworkInterface | Get-Member returns "Name          Property   string Name {get;set;}"

    What am I doing wrong? I've tried to change it in registry, but Failover Cluster Manager does not get it.


  • How would you leverage setting NIC configuration remotely?And, I am assuming I have to install windows powershell 3.0 on a windows 7/windows server 2008 R2 to execute those commands?


  • @Chris, unfortunately not. Even if you try "tricks" like installing PS3 and PS4 onto a 2008 r2 server and then remoting to it, it still doesn't understand the commands. For 2008 R2 and earlier, I still use netsh.