Nov 23, 2016

How to Manage NICs with PowerShell on Your Windows PC


Since Windows 8 and Server 2012, powershell gives an expansion of cmdlets that let you read the configuration of Network Adapters. In some scenarios you may additionally change settings such as the MAC Address, Wake-on-Lan, or protocols that are bound to the NICs.






In this guide, we'll walk you through the steps to manage network adapters on your Windows PC using PowerShell.

Contents:

  • Reading the configuration with Get-NetAdapter
  • Additional attributes with Get-NetAdapterAdvancedProperty
  • Configuring Wake-on-LAN
  • Displaying bound protocols
  • Restarting an adapter
On a Windows PC, you usually access the network adapter settings through the Graphical User Interface (GUI) where you can also change properties. However, on Server Core or Nano Server you have to rely on the command line. In addition to the netsh.exe tool, PowerShell is often the best choice – also because you can manage network interfaces remotely.

Reading the network interface's configuration

If you want to get an overview of the available NICs and their statuses, Get-NetAdapter offers the most important information. If you run the cmdlet without arguments, it reads all adapters and displays a few of their properties such as the MAC address, the status, and the link speed.

However, Get-NetAdapter can retrieve more than 100 properties that you can display with the help of Get-Member:

Get-NetAdapter -Name Ethernet0 | Get-Member -MemberType Property

This command only retrieves information from one interface to avoid displaying properties multiple times. The Name parameter also accepts wildcards, which allows you to access all interfaces that begin with “Ethernet”:

Get-NetAdapter -Name Ethernet*

Alternatively, you can use the parameter InterfaceDescription and InterfaceIndex to filter the output:

Get-NetAdapter -InterfaceDescription Real*

Respectively

Get-NetAdapter -InterfaceIndex 3

Once you have found the required properties of the MSFT_NetAdapter object using Get-Member, you can extract them as usual with Select-Object.

Get-NetAdapter -Name vEth* | select Name, MacAddress, MediaConnectionState, MediaType

You can get some of the attributes by using the parameters of Get-NetAdapter as filters, for instance to access only one physical adapter:

Get-NetAdapter -Physical

However, if you only want to retrieve virtual adapters, you again will need Select-Object:

Get-NetAdapter | ? Virtual -eq $true

You can list all interface properties with Format-List and the parameter Property.

Get-NetAdapter -Name vEth* | Format-List -Property


Additional attributes with Get-NetAdapterAdvancedProperty

However, even with this command, some attributes are still hidden, for example those for offloading various operations, the configuration of jumbo frames, or Wake-On-LAN. To read those properties, you need additional cmdlets such as Get-NetAdapter­Advanced­Property and Get-NetAdapter­Power­Management.


You can filter the adapter properties with these two cmdlets in the same way as with Get-NetAdapter by using the parameters Name and InterfaceDescription. Selecting the attributes works accordingly.


Configuring Wake-on-LAN

For example, if you want to read the Wake-On-LAN (WOL) settings on all physical interfaces whose names begin with “Eth,” you can use this command:

Get-NetAdapter -Physical -Name Eth* | Get-NetAdapterPowerManagement | select Name, WakeOnMagicPacket

If you want to enable WOL on all those adapters, Set-NetAdapterPowerManagement will do the job:

Set-NetAdapterPowerManagement -Name Eth* -WakeOnMagicPacket Enabled


Displaying bound protocols

If you require information about the protocols that are bound to certain adapters, you can work with Get-NetAdapterBinding:

Get-NetAdapterBinding -Name Eth* -AllBindings

This example will display all protocols that are bound to the adapter whose names begin with “Eth*.”

For example, if you want to retrieve the interfaces where IPv4 is not activated, then this commands helps:

Get-NetAdapterBinding -Name * | ? {$_.ComponentID -eq "ms_tcpip" -and $_.Enabled -eq $false}

You can use the properties DisplayName and ComponentID to change their status. The next command activates IPv4 on the adapter with the name vEthernet.

Set-NetAdapterBinding -Name vEthernet -ComponentID ms_tcpip -Enabled $true






Restarting an adapter

The cmdlet Restart-NetAdapter is useful if you have to modify settings. This corresponds to deactivating the interface and then activating it again through the GUI. It also ensures the renewal of the IP address through DHCP.

Post a Comment

 
TECH SUPPORT © 2012 - Designed by INFOSBIRD