Search code examples
powershellwifipower-management

How to change this powershell script to uncheck "allow the computer to turn off this device to save power" for Wi-Fi in addition to Ethernet


I have this script and it works to turn uncheck the box for the Ethernet adapter but no matter how I futz around with it I can't get it to do the same thing for Wi-Fi.

$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()
    }
}

Have tried removing all the AND NOT lines. Have tried a few other scripts I found online but to no avail.

$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 would want to to uncheck the "Allow the computer to turn off this device to save power" box under power management for the Wi-fi adapter under Network Connections.


Solution

  • You can use the Powershell cmdlet Get-NetAdapter to identify your WLAN interfaces. To do that you can use the property PhysicalMediaType.

    In this case you want to search for WLAN adapters, so match it with 'Native 802.11'. For older operating systems sometimes we have to match it with 'Wireless LAN'.

    To get all WLAN Adapter u can use this in your Script:

    $NICs = Get-NetAdapter | Where-Object {$_.PhysicalMediaType -eq 'Native 802.11' -or $_.PhysicalMediaType -eq 'Wireless LAN'}