When I'm connected to an external display, I would like to close the laptop without it going to sleep. When I'm not connected to an external display, I would like to have closing the lid put the laptop to sleep.
I know I can achieve this by manually switching the power settings, but I'd like something automatic. Any idea? Сan we track the external display connection?
Windows 10

  1. Determine (or create, if necessary) two power schemes, one with sleep button enabled, one with disabled.

  2. Using command powercfg /l determine GUID's of these schemes.

  3. Install AutoHotKey and set up launching this monitoring script after each start of Windows. Every time the monitor is connected and disconnected, AutoHotKey will run the script for you, switching the power scheme:

    OnMessage(0x219, "MsgMonitor")
    MsgMonitor(wParam, lParam, msg)
        if (wParam = 7) {
            Run, powercfg /s 381b4222-f694-41f0-9685-ff5bb260df2e
        } Else {
            Run, powercfg /s 381b4222-0001-2222-3333-000000000000
        MsgBox check %wParam% and %lParam% and decide to run programs with %msg%
    ;wParam: 7 lParam: 0  monitor connected
    ;wParam: 32772 lParam: 8977536 should be on disconected

Important: Replace sample GUID's in the above code with GUID's you determined in step 2.



@miroxlav solution did not work for me. I changed the script as follows.

  • You still have to create two power saving configs
  • The AutoHotKey script is typically executed at startup.
  • The caught event is a bit different (WM_DISPLAYCHANGE)
  • You have to identify your main monitor instance nam from powershell get-WmiObject or device manager or...
  • power config UUIDs are hard coded in script too.
       Please note that it is not sufficient to count the number of monitors because the
       main monitors goes off line when you close the lid.
       Which resets the count to... 1
       So instead, we just make our decision on the presence of a different monitor than the known
       main one (hardcoded id, SN is a poor criterion).

        Subscribe to windows event
    OnMessage(0x7E, "MsgMonitor")

    MsgMonitor(wParam, lParam, msg) {

    /* Sleep 2 sec because there is a delay before display is known to WMI */
    Sleep 2000

    /* default */
    strComputer := "."

    /* This is the one for my PC... */
    myMonitor := "DISPLAY\LGD056E\4&13419694&0&UID265988_0"

    objWMIService := ComObjGet("winmgmts:{impersonationLevel=impersonate}!\\" . strComputer . "\root\wmi")
    colItems := objWMIService.ExecQuery("Select * FROM WMIMonitorID")._NewEnum

    hasExternal := false

    While colItems[objItem]
    if objItem.instanceName != myMonitor {
        hasExternal := True

    if ( hasExternal ) {
        /* this is the power config that does not set laptop to sleep on lid closing */event
        Run, powercfg /s a48ebd52-0590-400d-b032-ac7f4302c0e1
    } Else {
        /* this instead is the power config that does set laptop to sleep on lid closing event */
        Run, powercfg /s 377a8558-bff4-4f51-ab43-626b1aa5a65f


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.