Managing network adapters using PowerShell

Introduction

When the network goes down, everything grinds to a halt. You can’t do your work, and your business can’t earn money. Always-on connectivity is a must in today’s deeply interconnected world. This makes being able to properly configure, manage and troubleshoot network adapters, both wired and wireless, a must for the system administrator. There are numerous different kinds of tasks that fall under the general heading of network interface management tasks: configuring interfaces, IP addresses, default gateways, routes, and metrics; configuring ISATAP and Teredo for IPv4/IPv6 interoperability; and so on. Then there are the higher level task that are needed at the level of network services such as configuring DHCP scopes, options, and reservations; creating different types of DNS zones; configuring DNS root hints and forwarders; creating resource records; and so on.

In enterprise environments, it’s important to be able to automate common system and network administration tasks such as the ones listed above. That’s why Windows PowerShell is so important to those who administer Windows Server-based networks. With the PowerShell capabilities built into Windows Server 2012 and Windows Server 2012 R2, you can perform most network-administration tasks from the PowerShell command line or by running PowerShell scripts. The best way to learn how to use Windows PowerShell to administer network settings and services on Windows Server 2012 and Windows Server 2012 R2 is to experiment with performing different tasks in a test environment. The sections that follow provide some examples of what you can do in this area. The explanation and procedures included below are adapted from my book Training Guide: Installing and Configuring Windows Server 2012 R2 (Microsoft Press, 2014). Also included near the end of this article are a few additional tips on this subject that I’ve gleaned from the larger IT pro community including the almost 100,000 followers of our WServerNews weekly newsletter which you can subscribe to at http://www.wservernews.com/subscribe.htm.

Displaying network adapters with 100 Mbps link speed

You can use the Get-NetAdapter cmdlet to display all network adapters on the server that have a link speed of 100 megabits per second (Mbps) like this:

PS C:\> Get-NetAdapter | Where-Object -FilterScript {$_.LinkSpeed -eq "100 Mbps"}
 
Name        InterfaceDescription       ifIndex Status  MacAddress         LinkSpeed
----         --------------------      ------- ------  ----------         ---------
Ethernet 2  Broadcom NetXtreme Gig...  13 Up           A4-BA-DB-0A-96-0C  100 Mbps
Ethernet    Broadcom NetXtreme Gig...  12 Up           A4-BA-DB-0A-96-0B  100 Mbps

The output of this command consists of objects that can be passed through the pipeline to other cmdlets. For example, you could pipe the output into the Set-NetIPInterface cmdlet to assign a metric value of 5 to all interfaces having a link speed of 100 Mbps as follows:

PS C:\> Get-NetAdapter | Where-Object -FilterScript {$_.LinkSpeed -eq "100 Mbps"} | `
Set-NetIPInterface -InterfaceMetric 5

Disabling a binding on a network adapter

You can enable and disable bindings on a network adapter using Windows PowerShell. For example, start by using the Get-NetAdapterBinding cmdlet to display the bindings for the specified interface:

PS C:\> Get-NetAdapterBinding -InterfaceAlias "Ethernet 2"
 
Name        DisplayName                                        ComponentID  Enabled
----        -----------                                        -----------  -------
Ethernet 2  Hyper-V Extensible Virtual Switch                  vms_pp       False
Ethernet 2  Link-Layer Topology Discovery Responder            ms_rspndr    True
Ethernet 2  Link-Layer Topology Discovery Mapper I/O Driver    ms_lltdio    True
Ethernet 2  Microsoft Network Adapter Multiplexor Protocol     ms_implat    False
Ethernet 2  Client for Microsoft Networks                      ms_msclient  True
Ethernet 2  Windows Network Virtualization Filter driver       ms_netwnv    False
Ethernet 2  QoS Packet Scheduler                               ms_pacer     True
Ethernet 2  File and Printer Sharing for Microsoft Networks    ms_server    True
Ethernet 2  Internet Protocol Version 6 (TCP/IPv6)             ms_tcpip6    True
Ethernet 2  Internet Protocol Version 4 (TCP/IPv4)             ms_tcpip     True
To disable a specific binding such as QoS Packet Scheduler, you can use the DisableNetAdapterBinding cmdlet, like this:
PS C:\> Disable-NetAdapterBinding -Name "Ethernet 2" -ComponentID ms_pacer

You can use the Enable-NetAdapterBinding cmdlet to reenable the binding.

Disabling a network adapter

You can disable a specific network adapter or even all network adapters using Windows PowerShell. For example, the following command disables the adapter named Ethernet 2 with no confirmation prompt displayed:

PS C:\> Disable-NetAdapter -Name "Ethernet 2" -Confirm:$false

To disable all network adapters on the server, you can use this command:

PS C:\> Disable-NetAdapter -Name *

Note that all remote connectivity with the server will be lost if you do this.

To enable any network adapters that are disabled, you can use the Enable-NetAdapter cmdlet.

Creating a DHCP server scope

You can manage Windows Server 2012 or Windows Server 2012 R2 DHCP servers using Windows PowerShell. Common DHCP server-management tasks include creating scopes, creating exclusion ranges, creating reservations, configuring scope and server options, and so on.

For example, let’s begin by viewing all the scopes currently configured on the DHCP server:

PS C:\> Get-DhcpServerv4Scope
 
ScopeId      SubnetMask      Name  State   StartRange   EndRange      LeaseDuration
-------      ----------      ----  -----   ----------   --------      -------------
172.16.11.0  255.255.255.0   test  Active  172.16.11.35 172.16.11.39  8.00:00:00

Note that there is currently only one active scope on the DHCP server. Now add a second scope for the IP address range 172.16.12.50 through 172.16.11.100. Leave the scope inactive until you finish configuring exclusions and reservations for it:

PS C:\> Add-DhcpServerv4Scope -EndRange 172.16.12.100 -Name test2 `
-StartRange 172.16.12.50 -SubnetMask 255.255.255.0 -State InActive

Note that in this cmdlet the order in which you specify the parameters doesn’t matter because you specified the end of the address range before specifying its beginning.

Running Get-DdhpServerv4Scope again indicates that adding the new scope was successful:

PS C:\> Get-DhcpServerv4Scope
 
ScopeId      SubnetMask      Name   State    StartRange    EndRange      LeaseDuration
-------      ----------      ----   -----    ----------    --------      -------------
172.16.11.0  255.255.255.0   test   Active   172.16.11.35  172.16.11.39  8.00:00:00
172.16.12.0  255.255.255.0   test2  Inactive 172.16.12.50  172.16.12.100 8.00:00:00
Now exclude the range 172.16.12.70 through 172.16.12.75 from the new scope:
PS C:\> Add-DhcpServerv4ExclusionRange -EndRange 172.16.12.75 -ScopeId 172.16.12.0 `
   -StartRange 172.16.12.70
Let’s also add a reservation for a file server:
PS C:\> Add-DhcpServerv4Reservation -ClientId EE-05-B0-DA-04-00 -IPAddress 172.16.12.88 `
   -ScopeId 172.16.12.0 -Description "Reservation for file server"

Here, EE-05-B0-DA-04-00 represents the MAC address of the file server’s network adapter.

Let’s also configure a default gateway address for the new scope by creating a scope option as follows:

PS C:\> Set-DhcpServerv4OptionValue -Router 172.16.12.1 -ScopeId 172.16.12.0

If you want to create a server option instead of a scope option, you could do this by omitting the –ScopeID parameter from the preceding command.

Now you’re done creating and configuring the new scope, so let’s finish by activating it:

PS C:\> Set-DhcpServerv4Scope -State Active

Creating DNS resource records

You can manage Windows Server 2012 or Windows Server 2012 R2 DNS servers using Windows PowerShell. Common DNS server-management tasks include adding resource records to zones, configuring forwarders, configuring root hints, and so on.

For example, let’s view a list of zones on a DNS server that is also a domain controller for the corp.contoso.com domain:

PS C:\> Get-DnsServerZone
 
ZoneName                 ZoneType  IsAutoCreated   IsDsIntegrated  IsRever...  IsSigned
--------                 --------  -------------   --------------  -------     --------
_msdcs.corp.contoso.com  Primary   False           True            False       True
0.in-addr.arpa           Primary   True            False           True        False
127.in-addr.arpa         Primary   True            False           True        False
255.in-addr.arpa         Primary   True            False           True        False
corp.contoso.com         Primary   False           True            False       False
TrustAnchors             Primary   False           True            False       False

To view a list of resource records of type A (address) in the corp.contoso.com zone, you can pipe the output of the Get-DnsServerResourceRecord cmdlet into the Where-Object cmdlet, like this:

PS C:\> Get-DnsServerResourceRecord -ZoneName corp.contoso.com | Where-Object {$_.RecordType -eq "A"}
 
HostName         RecordType Timestamp            TimeToLive      RecordData
--------         ---------- ---------            ----------      ----------
@                A          7/8/2012 12:00:00 PM 00:10:00        172.16.11.36
@                A          7/8/2012 1:00:00 PM  00:10:00        172.16.11.232
DomainDnsZones   A          7/8/2012 12:00:00 PM 00:10:00        172.16.11.36
DomainDnsZones   A          7/8/2012 12:00:00 PM 00:10:00        172.16.11.232
ForestDnsZones   A          7/8/2012 12:00:00 PM 00:10:00        172.16.11.36
ForestDnsZones   A          7/8/2012 12:00:00 PM 00:10:00        172.16.11.232
sea-srv-1        A          0                    01:00:00        172.16.11.232
SEA-SRV-5        A          0                    01:00:00        172.16.11.36

To add a new A resource record for a test server, you can use the Add-DnsServerResourceRecordA cmdlet, like this:

PS C:\> Add-DnsServerResourceRecordA -IPv4Address 172.16.11.239 -Name SEA-TEST `
-ZoneName corp.contoso.com

You can also add other types of resource records—such as PTR, CN, or MX records—using the preceding cmdlet. And you can use the Remove-DnsServerResourceRecord cmdlet to remove resource records from a zone.

Some Additional Tips

Finally, here are a few more tips on this subject that I’ve gleaned from my colleagues in IT and from readers of our newsletter WServerNews.com.

Displaying physical network adapters

If you only want to display a list of physical network adapters on your system, you can use this command:

Get-NetAdapter –Physical

Displaying hidden network adapters

If you want also show any hidden network adapters, do it like this:

Get-NetAdapter –IncludeHidden

Displaying virtual network adapters

If you want to display only the virtual network adapters you can do it this way:

Get-NetAdapter | Where {$_.Virtual –eq $True}

Displaying DNS client configuration

If you need to know whether the DNS client on a machine is configured as static or dynamic, you can use this command to find out:

Get-NetIPAddress | where {$_.PrefixOrigin -eq “DHCP” -or $_.SuffixOrigin -eq “DHCP”}

The above command works in PowerShell v3 or higher which is available on Windows Server 2012 and Windows 8 and higher. If you’re still using PowerShell v2 on Windows Server 2008 R2 and Windows 7 then you can do it this way:

Get-WmiObject win32_networkadapterconfiguration | where {$_.IPEnabled -and $_.DHCPEnabled}

Not sure which version of PowerShell you’re using on a system? Type this command at the PowerShell prompt to find out:

$PSVersionTable.PSVersion

Displaying IP configuration

Sometimes this is easy, sometimes not. For example, if you want to display only the InterfaceIndex, InterfaceAlias and IPv4Address properties for each adapter, you can do it like this:

Get-NetIpConfiguration | Select-Object interfaceindex, interfacealias, Ipv4address

But if you also want to display the IPv4DefaultGateway property in an intelligible way, you need to do it like this:

Get-NetIpConfiguration | Select-Object interfaceindex, interfacealias, Ipv4address, @{ Label=”DefaultGateway”; Expression={ $_.IPv4DefaultGateway.NextHop } }

The same is true if you want to include the DNSServer property in your command output:

Get-NetIpConfiguration | Select-Object interfaceindex, interfacealias, Ipv4address, @{ Label=”DefaultGateway”; Expression={ $_.IPv4DefaultGateway.NextHop } }, @{ Label=”DnsServers”; Expression={ $_.DnsServer.ServerAddresses } }

If you’d rather display your output in tabular form, you can do it like this:

Get-NetIpConfiguration | format-table interfaceindex, interfacealias, Ipv4address, @{ Label=”DefaultGateway”; Expression={ $_.IPv4DefaultGateway.NextHop } }, @{ Label=”DnsServers”; Expression={ $_.DnsServer.ServerAddresses } }

Note however that using format-table like this causes the returned object data to be lost, which means you won’t be able to do any further sorting or filtering on your command output. So if for example you want to export your output to a CSV file so you can open it in Excel, you should do it this way:

Get-NetIpConfiguration | format-table interfaceindex, interfacealias, Ipv4address, @{ Label=”DefaultGateway”; Expression={ $_.IPv4DefaultGateway.NextHop } }, @{ Label=”DnsServers”; Expression={ $_.DnsServer.ServerAddresses } } | Export-CSV .\output.csv

About The Author

3 thoughts on “Managing network adapters using PowerShell”

  1. Could you please provide equivalent command for Windows 2008 Machines as the following is not working on Win 2008 Machines

    Enable-NetAdapterBinding -Name “Ethernet” -ComponentID ms_server

Leave a Comment

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top