Use PowerShell to Work with Network Adapter Power Settings
Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to work with network adapter power settings.
Microsoft Scripting Guy, Ed Wilson, is here. Today I want to continue my network adapter series, but first…
PowerShell Saturday #007 will be held in Charlotte, North Carolina on February 8, 2014. This will be an awesome chance to meet and to learn from some of the best PowerShellers around. In fact, five of the speakers are also speakers at the PowerShell Summit this year. There are a limited number of tickets still available for this event, so you’ll want to sign up now. The Scripting Wife wrote a great post that provides a quick overview of the event: Psst…Charlotte PowerShell Saturday Details Leaked.
Note This is the fifth post in a series that examines working with network adapters. You may want to refer to the
earlier posts to get a feel for the way the series progresses:
- Use PowerShell to Identify Network Adapter Characteristics
- Enabling and Disabling Network Adapters with PowerShell
- Renaming Network Adapters by Using PowerShell
- Using PowerShell to Find Connected Network Adapters
In Windows 7, the network adapter power settings expanded. Some of the improvements are listed here:
- Wake on LAN and wake on wireless LAN. Improved wake patterns reduce the number of false wakes. A directed packet (such as a ping) does not cause the computer to wake up.
- Address Resolution Protocol (ARP) and Neighbor Solicitation (NS) offload. ARP and NS packets do not wake up the computer. Instead, the network adapter can respond. Therefore, the computer does not need to wake up to maintain a presence on the network. This support depends on NDIS 6.0 drivers, and it may not be available with older hardware.
- Low power on media disconnect. Enables the computer to place the network adapter into a low power state when the network cable is unplugged and the computer is running.
For more information, see Power Management for Network Devices in Windows 7.
These settings are configurable via the graphical user interface by clicking the Configure button from the Network Adapter Properties dialog box. The settings are shown in the image that follows:
Some of the network adapter power management settings are configurable via Netsh. For example, to permit ARP packets and NS packets to wake the network adapter, I would use a command such as:
netsh interface ipv4 set interface 12 forcearpndwolpattern=enabled
When the command completes successfully, it returns OK. Keep in mind that this will also cause a network adapter reset. The command and its associated output are shown here:
Using the NetAdapter module
To query the power management settings for a specific network adapter, use the Get-NetAdapterPowerManagement function and specify the name of the network adapter. An example of this command is shown here:
Get-NetAdapterPowerManagement -Name ethernet
The command and the output associated with the command are shown in the image that follows:
The Get-NetAdapterPowerManagement function only permits the use of the adapter name or interface description as parameters. But the Get-NetAdapter function is much more flexible. I often use Get-NetAdapter to retrieve a specific network adapter, and then pipe it to other functions such as Get-NetAdapterPowerManagement. This technique is shown here:
Get-NetAdapter -InterfaceIndex 4 | Get-NetAdapterPowerManagement
To configure network adapter power management, I use the Set-NetAdapterPowerManagement function. Once again, I want to retrieve my network adapter by interface index number instead of having to type the name or description of the adapter. I pipe the resulting network adapter object to the Set-NetAdapterPowerManagement function and specify a value for the –WakeOnMagicPacket parameter. The command is shown here:
Get-NetAdapter -InterfaceIndex 4 | Set-NetAdapterPowerManagement -WakeOnMagicPacket Enabled
Because no output returns from the command, I use the Get-NetAdapter command a second time to verify that the configuration change took place. The commands and their associated output are shown in the following image:
It so happens that I know what the permissible values are for the parameters. But if I did not know this, I could create the command in the Windows PowerShell ISE and rely on the Intellisense features. When I type a parameter name, the permissible values appear and make it possible to select the correct value from the list, as shown here:
Most of the time, when I need to manage network adapter power management settings, it is because of a new deployment, or because an audit has determined that I have configuration drift. (Hmmm…this would actually be a great thing to use desired configuration management to control.) So, I put all the settings I want to configure into a single command, such as this:
Set-NetAdapterPowerManagement -Name ethernet -ArpOffload Enabled -DeviceSleep
OnDisconnect Disabled -NSOffload Enabled -WakeOnMagicPacket Enabled -WakeOnPattern Enabled -PassThru
The –passthru parameter outputs a configuration management object so I can inspect it to ensure that the proper changes occurred. The command and the output from the command are shown that follows:
To make changes to multiple computers, I first use the New-CimSession cmdlet to make my remote connections. I can specify the computer names and the credentials to use to make the connection. I then store the remote connection in a variable.
Next, I pass that CIM session to the –CimSession parameter. The key to remember is that I must be able to identify the network adapter that I need to use for the management activity. An example of creating a CIM session and using it follows. (This is a two-line command, so if you directly copy it, you must change the computer name and network interface name, and remove spaces until the second command appears on a single line.)
$session = New-CimSession -ComputerName edlt
Set-NetAdapterPowerManagement -CimSession $session -name ethernet -ArpOffload
Enabled -DeviceSleepOnDisconnect Disabled -NSOffload Enabled -WakeOnMagicPacket
Enabled -WakeOnPattern Enabled -PassThru
The command and the output from the command are shown in the image that follows.
Note These commands require that the Windows PowerShell console or the Windows PowerShell ISE is opened with Admin rights. To do this, right-click the Windows PowerShell console icon or the Windows PowerShell ISE icon while holding down the Shift key, and select Run as administrator. Or if you launch it via Windows Search in Windows 8.1, type Windows PowerShell from the Start page, and the Search dialog box appears with the Windows PowerShell icon. Right-click the icon, and select Run as administrator from the Action menu.
If you do not launch Windows PowerShell with Admin rights, an error message appears that states it cannot find the network adapter. An example of the error message is shown here:
Network Adapter Week will wrap-up tomorrow. I will be talking about how to gather network statistics.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at email@example.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy