Device Wake Config tool

Have you ever had to peruse your Device Manager trying to figure out which device is waking your computer from sleep? Isn’t it annoying how you have to open each device one by one, checking the Power Management tab and look for that checkbox?

Well, I found it annoying. So I wrote a little Powershell script that gives me a GUI with a list of all the wakeable devices so I can easily toggle off/on whatever I want.


Add-Type -namespace Win32 -name UI -MemberDefinition '[DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);'
[void][Win32.UI]::ShowWindowAsync((Get-Process -PID $PID).MainWindowHandle, 0) # Hide console window

Add-Type -AssemblyName PresentationFramework, System.Drawing, System.Windows.Forms
[Windows.Forms.Application]::EnableVisualStyles()

$ItemCheck_Handler = {
    $Form_Main.Cursor = "WaitCursor"
    $SelItem = $Lv_DeviceList.Items[$_.Index]
    If ($_.NewValue -eq 'Checked') {
        Start-Process powercfg -ArgumentList "/deviceenablewake ""$($SelItem.Text)""" -Wait -WindowStyle Hidden
    } Else {
        Start-Process powercfg -ArgumentList "/devicedisablewake ""$($SelItem.Text)""" -Wait -WindowStyle Hidden
    }
    UpdateWakeArms
    $Form_Main.Cursor = "Default"
}

$WakeableDevices = @(powercfg /devicequery wake_programmable) | Sort-Object

$Form_Main = New-Object Windows.Forms.Form -Property @{
    Text = 'Device Wake Config'
    ClientSize = '300,400'
    MaximizeBox = $False
    MinimizeBox = $False
    SizeGripStyle = 'Show'
}

$Form_Main.Controls.Add(($Lv_DeviceList = New-Object Windows.Forms.ListView -Property @{
    Bounds = "0,0,300,400"
    Anchor = 'Top,Left,Bottom,Right'
    View = 'Details'
    FullRowSelect = $True
    Multiselect = $False
    HideSelection = $False
    HeaderStyle = 'Nonclickable'
    CheckBoxes = $True
   
    add_ItemCheck = $ItemCheck_Handler
}))
[void]$Lv_DeviceList.Columns.Add('Device Name', -1)

Foreach ($Device in $WakeableDevices) {
    If ($Device.trim() -ne "") {
        $Item = $Lv_DeviceList.Items.Add($Device)
    }
}
$Lv_DeviceList.AutoResizeColumns(2)

Function UpdateWakeArms() {
    $WakeArmed = @(powercfg /devicequery wake_armed)
    $Lv_DeviceList.remove_ItemCheck($ItemCheck_Handler)
    Foreach ($Item in $Lv_DeviceList.Items) {
        $Checked = $WakeArmed.Contains($Item.Text)
        If ($Item.Checked -ne $Checked) {
            $Item.Checked = $Checked
        }
    }
    $Lv_DeviceList.add_ItemCheck($ItemCheck_Handler)
}
UpdateWakeArms

[void]$Form_Main.ShowDialog()
This entry was posted in Coding and tagged , . Bookmark the permalink.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.