'Disable turn off this device to save power for NIC

$NICs = Get-WmiObject Win32_NetworkAdapter -filter "AdapterTypeID = '0' AND PhysicalAdapter = 'true' AND NOT Description LIKE '%wireless%' AND NOT Description LIKE '%virtual%' AND NOT Description LIKE '%WiFi%' AND NOT Description LIKE '%Bluetooth%'"
Foreach ($NIC in $NICs)
{
    $powerMgmt = Get-WmiObject MSPower_DeviceEnable -Namespace root\wmi | where {$_.InstanceName -match [regex]::Escape($nic.PNPDeviceID)}
    If ($powerMgmt.Enable -eq $True)
    {
         $powerMgmt.Enable = $False
         $powerMgmt.psbase.Put()
    }
}

I have this code, and i works, but PSAnalyzer says "For PowerShell 3.0 and above, use CIM cmdlet which perform the same tasks as the WMI cmdlets. The CIM cmdlets comply with WS-Management (WSMan) standards and with the Common Information Model (CIM) standard, which enables the cmdlets to use the same techniques to manage Windows computers and those running other operating systems." How to rewrite it using CIM cmdlet?



Solution 1:[1]

By Get-NetAdapter -Physical we get all physical adapters. And through pipe get its' NetAdapterPowerManagement ('Get-NetAdapterPowerManagement'). There is a property AllowComputerToTurnOffDevice that is responsible for the button "Turn off this device to save power". And in the end we set it to 'Disabled'.

if ((Get-CimInstance -ClassName Win32_ComputerSystem).PCSystemType -ne 2)
{
    $adapters = Get-NetAdapter -Physical | Get-NetAdapterPowerManagement | Where-Object -FilterScript {$_.AllowComputerToTurnOffDevice -ne "Unsupported"}
    foreach ($adapter in $adapters)
    {
        $adapter.AllowComputerToTurnOffDevice = "Disabled"
        $adapter | Set-NetAdapterPowerManagement
    }
}

Solution 2:[2]

This works for me:

 $adapters = Get-NetAdapter -Physical | Get-NetAdapterPowerManagement
    foreach ($adapter in $adapters)
        {
        $adapter.AllowComputerToTurnOffDevice = 'Disabled'
        $adapter | Set-NetAdapterPowerManagement
        }

Solution 3:[3]

something like this should work fine for you.

foreach ($NIC in (Get-NetAdapter -Physical)){
    $PowerSaving = Get-CimInstance -ClassName MSPower_DeviceEnable -Namespace root\wmi | ? {$_.InstanceName -match [Regex]::Escape($NIC.PnPDeviceID)}
    if ($PowerSaving.Enable){
        $PowerSaving.Enable = $false
        $PowerSaving | Set-CimInstance
    }
}

Solution 4:[4]

I used the great examples in this post to solve a request to only disable power save mode on wireless adapters. It filters for just wireless adapters and displays the results for each instance that it finds. The setting that I'm changing in the user interface is located at >> Device Manager> Network Adapters> Wireless Adapter> Properties> Power Management> clear check box for - Allow the computer to turn off this device to save power

# List all available properties in NetAdapter cmdlets
Get-NetAdapter | Get-Member | Format-Table
Get-NetAdapterPowerManagement | Get-Member | Format-Table

# Find Wireless adapter and disable power save mode
Get-NetAdapter -Physical | `
Where-Object {$_.InterfaceDescription -like "*Wireless*" `
  -or $_.interfaceName -like "*wireless*" `
  -or $_.Name -eq "Wi-Fi" `
  -or $_.PhysicalMediaType -like "*802.11*"} | `
Get-NetAdapterPowerManagement | `
ForEach-Object {
    $_.AllowComputerToTurnOffDevice = 'Disabled' 
    $_ | Set-NetAdapterPowerManagement
   ( Get-NetAdapterPowerManagement | `
     Where-Object {$_.InterfaceDescription -like "*Wireless*" `
    -or $_.Name -eq "Wi-Fi"} | `
    Format-List -Property Name,InterfaceDescription,AllowComputerToTurnOffDevice) 
    }

Solution 5:[5]

I know this is an old discussion, but as is often the case, I stumbled on it while looking to see how others had solved the problem I had just worked on. In my case I was working against remote systems so the -CimSession parameter is included, but here's what I came up with:

$ComputerName = "SomeComputer"
Get-NetAdapter -CimSession $ComputerName -Physical |
Get-NetAdapterPowerManagement |
ForEach-Object{
    $_.AllowComputerToTurnOffDevice = 'Disabled' 
    $_ | Set-NetAdapterPowerManagement
}

The above code worked perfectly, though I did observe a very brief disconnect in the Cim session. That said, the RDP session on my other screen didn't even flinch. However, if concerned you could probably get away with adding a Start-Sleep command after the Set-NetAdapterPowerManagement line.

I was doing this for a single one off job so I didn't bother building it up any further.

I'd add that there may be additional concerns if NIC teaming is configured. In my test case I have 4 NICs 2 of them are enabled and part of a team, 2 of them are disabled.

Get-NetAdapter | Format-Table -AutoSize

# Output:

Name       InterfaceDescription                         ifIndex Status      MacAddress        LinkSpeed
----       --------------------                         ------- ------      ----------        ---------
Ethernet 4 HP Ethernet 1Gb 4-port 331FLR Adap...#4           15 Not Present AC-16-2D-71-F7-DF     0 bps
Ethernet   HP Ethernet 1Gb 4-port 331FLR Adap...#3           12 Up          AC-16-2D-71-F7-DE    1 Gbps
Ethernet 2 HP Ethernet 1Gb 4-port 331FLR Adap...#2           13 Disabled    AC-16-2D-71-F7-DD    1 Gbps
Ethernet 3 HP Ethernet 1Gb 4-port 331FLR Adapter             14 Up          AC-16-2D-71-F7-DC    1 Gbps
TEAM 0     Microsoft Network Adapter Multiplexor Driver      18 Up          AC-16-2D-71-F7-DC    1 Gbps

Obviously there are 4 ports treated as independent adapters, but look at the results when adding the -Physical Parameter:

Get-NetAdapter -Physical | Format-Table -AutoSize

# Output:
Name       InterfaceDescription                    ifIndex Status   MacAddress        LinkSpeed
----       --------------------                    ------- ------   ----------        ---------
Ethernet   HP Ethernet 1Gb 4-port 331FLR Adap...#3      12 Up       AC-16-2D-71-F7-DE    1 Gbps
Ethernet 2 HP Ethernet 1Gb 4-port 331FLR Adap...#2      13 Disabled AC-16-2D-71-F7-DD    1 Gbps
Ethernet 3 HP Ethernet 1Gb 4-port 331FLR Adapter        14 Up       AC-16-2D-71-F7-DC    1 Gbps

I'm speculating but it looks like teaming somehow consumes the #4 adapter, even though it's disabled. That makes me a little uncomfortable, and perhaps is grounds for more research. However, I'll say running the top-most codeonly works against enabled adapters anyhow, so even on the teamed server I got the desired result and didn't notice any additional side effects.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Steven
Solution 2 odoron
Solution 3 colsw
Solution 4
Solution 5