PowerShell: Enable wakeup for devices

Introduction

Sometimes, if you allow your computers to go to sleep regularly, you may want to ensure that they can be easily woken using their keyboards, mice, or other peripherals. End users can get confused if they are only able to wake their computer by pressing the power button; sometimes this is made even more inconvenient if their desktop computer is hidden away under their desk. In the interest of allow users to wake their computers using peripherals, we can deploy a script to enable those devices to be awoken.

Script Usage

The script included below, can be used by calling it like this:

'.Enable Wake for Devices.ps1' <PartOfDeviceName>

For example, I have two devices on my computer that I would like to enable to wake it up:

  • Logitech HID-Compliant Keyboard
  • Logitech HID-compliant Cordless Mouse

You can simply type a part of the device name(s), as the only argument to the script.

'.Enable Wake for Devices.ps1' Logitech

When I run the script on my computer, I receive the following output, confirming that it ran successfully. It will need to be executed as an account with administrative permissions, otherwise the script will fail.

Enabling wake-up for device: Logitech HID-compliant Cordless Mouse
Successfully updated device: Logitech HID-compliant Cordless Mouse
Enabling wake-up for device: Logitech HID-Compliant Keyboard
Successfully updated device: Logitech HID-Compliant Keyboard

Script

##############################################################################
#
#            Name: Enable Wake for Devices.ps1
#
#          Author: Trevor Sullivan
#
#            Date: August 18th, 2010
#
#         Purpose: Takes a string input, and enables wake-on-lan for the
#                  devices that match the input string. The script more or
#                  less matches up rootcimv2:Win32_PnpEntity instances with
#                  instances of the rootwmi:MSPower_DeviceWakeEnable class.
#
##############################################################################

param(
    [Parameter(Mandatory=$true,Position=0,ValueFromPipeline=$true)]
    [string]
    $PartialDeviceName
)

# Variable to store an array of devices to enable
$DeviceList = @()

function GetDevices()
{
    $WakeableDevices = Get-WmiObject -Namespace rootwmi -Class MSPower_DeviceWakeEnable

    foreach ($WakeableDevice in $WakeableDevices)
    {
        # Create a new, temporary device
        $NewDevice = CreateDevice
        $NewDevice.InstanceName = $WakeableDevice.InstanceName
        $NewDevice.EnableWake = $WakeableDevice.Enable
        $NewDevice.WmiPath = $WakeableDevice.__PATH
        $WqlQuery = "select Description from MSWmi_ProviderInfo where InstanceName = '$($NewDevice.InstanceName.Replace("","\"))'"
        $NewDevice.Name = (Get-WmiObject -Namespace rootwmi -Query $WqlQuery).Description

        $DeviceList += $NewDevice
    }

    Write-Output $DeviceList
}

function EnableDevices($Criteria)
{
    foreach ($Device in $DeviceList)
    {
        if ($Device.Name -match $Criteria)
        {
            Write-Host "Enabling wake-up for device: $($Device.Name)"
            $WmiDevice = [wmi]$Device.WmiPath
            $WmiDevice.Enable = $true
            if ($WmiDevice.Put()) {
                Write-Host "Successfully updated device:"  $Device.Name
            }
        }
    }
}

# Creates a new PSCustomObject to represent each device
function CreateDevice()
{
    $NewDevice = New-Object PSCustomObject
    # The InstanceName is the key property on the rootwmi:MSPower_DeviceWakeEnable WMI class
    Add-Member -InputObject $NewDevice -MemberType NoteProperty -Value "" -Name InstanceName
    Add-Member -InputObject $NewDevice -MemberType NoteProperty -Value "" -Name Name
    Add-Member -InputObject $NewDevice -MemberType NoteProperty -Value "" -Name EnableWake
    Add-Member -InputObject $NewDevice -MemberType NoteProperty -Value "" -Name WmiPath
    return $NewDevice
}

$DeviceList = . GetDevices
EnableDevices $PartialDeviceName