Turn monitors on and off



  • Turn monitors off:

    powershell (Add-Type '[DllImport(\"user32.dll\")]^public static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam);' -Name a -Pas)::SendMessage(-1,0x0112,0xF170,2)
    

    Turn monitors on (didn't work for me - see below):

    powershell (Add-Type '[DllImport(\"user32.dll\")]^public static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam);' -Name a -Pas)::SendMessage(-1,0x0112,0xF170,-1)
    

    In my case, the Turn monitors on powershell command didn't work for me. My monitors turned on but then turned right back off for some reason. So instead, I used this script and made this my command line so it effectively presses the Print Scrn button to turn my monitors back on:

    wscript d:\scripts\key.vbs PRTSC


  • @Russ said in Turn monitors on and off:

    powershell (Add-Type '[DllImport("user32.dll")]^public static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam);' -Name a -Pas)::SendMessage(-1,0x0112,0xF170,2)

    When I try and run the turn monitors off command, it works briefly, but my screen wakes up after maybe three seconds. While my monitors are off my keyboard and mouse flash rapidly at me. Any idea on how to fix this?



  • @Trenton-Lawson unfortunately I think the problem is specific to your hardware.



  • I get an error when running the monitor off command:

    PS C:\Windows\System32\WindowsPowerShell\v1.0> powershell (Add-Type '[DllImport(\"user32.dll\")]^public static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam);' -Name a -Pas)::SendMessage(-1,0x0112,0xF170,2)
    Add-Type : c:\Users\xxx\AppData\Local\Temp\xzybaz5r.0.cs(8) : Unexpected character '\'
    c:\Users\xxx\AppData\Local\Temp\xzybaz5r.0.cs(7) :     {
    c:\Users\xxx\AppData\Local\Temp\xzybaz5r.0.cs(8) : >>>     [DllImport(\"user32.dll\")]^public static extern int
    SendMessage(int hWnd, int hMsg, int wParam, int lParam);
    c:\Users\xxx\AppData\Local\Temp\xzybaz5r.0.cs(9) :
    At line:1 char:13
    + powershell (Add-Type '[DllImport(\"user32.dll\")]^public static exter ...
    +             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidData: (Microsoft.Power...peCompilerError:AddTypeCompilerError) [Add-Type], Except
       ion
        + FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand
    
    Add-Type : c:\Users\xxx\AppData\Local\Temp\xzybaz5r.0.cs(8) : Newline in constant
    c:\Users\xxx\AppData\Local\Temp\xzybaz5r.0.cs(7) :     {
    c:\Users\xxx\AppData\Local\Temp\xzybaz5r.0.cs(8) : >>>     [DllImport(\"user32.dll\")]^public static extern int
    SendMessage(int hWnd, int hMsg, int wParam, int lParam);
    c:\Users\xxx\AppData\Local\Temp\xzybaz5r.0.cs(9) :
    At line:1 char:13
    + powershell (Add-Type '[DllImport(\"user32.dll\")]^public static exter ...
    +             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidData: (Microsoft.Power...peCompilerError:AddTypeCompilerError) [Add-Type], Except
       ion
        + FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand
    
    Add-Type : c:\Users\xxx\AppData\Local\Temp\xzybaz5r.0.cs(8) : Invalid expression term ''
    c:\Users\xxx\AppData\Local\Temp\xzybaz5r.0.cs(7) :     {
    c:\Users\xxx\AppData\Local\Temp\xzybaz5r.0.cs(8) : >>>     [DllImport(\"user32.dll\")]^public static extern int
    SendMessage(int hWnd, int hMsg, int wParam, int lParam);
    c:\Users\xxx\AppData\Local\Temp\xzybaz5r.0.cs(9) :
    At line:1 char:13
    + powershell (Add-Type '[DllImport(\"user32.dll\")]^public static exter ...
    +             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidData: (Microsoft.Power...peCompilerError:AddTypeCompilerError) [Add-Type], Except
       ion
        + FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand
    
    Add-Type : c:\Users\xxx\AppData\Local\Temp\xzybaz5r.0.cs(8) : ) expected
    c:\Users\xxx\AppData\Local\Temp\xzybaz5r.0.cs(7) :     {
    c:\Users\xxx\AppData\Local\Temp\xzybaz5r.0.cs(8) : >>>     [DllImport(\"user32.dll\")]^public static extern int
    SendMessage(int hWnd, int hMsg, int wParam, int lParam);
    c:\Users\xxx\AppData\Local\Temp\xzybaz5r.0.cs(9) :
    At line:1 char:13
    + powershell (Add-Type '[DllImport(\"user32.dll\")]^public static exter ...
    +             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidData: (Microsoft.Power...peCompilerError:AddTypeCompilerError) [Add-Type], Except
       ion
        + FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand
    
    Add-Type : c:\Users\xxx\AppData\Local\Temp\xzybaz5r.0.cs(8) : Invalid token '"user32.dll")]^public stati...' in
    class, struct, or interface member declaration
    c:\Users\xxx\AppData\Local\Temp\xzybaz5r.0.cs(7) :     {
    c:\Users\xxx\AppData\Local\Temp\xzybaz5r.0.cs(8) : >>>     [DllImport(\"user32.dll\")]^public static extern int
    SendMessage(int hWnd, int hMsg, int wParam, int lParam);
    c:\Users\xxx\AppData\Local\Temp\xzybaz5r.0.cs(9) :
    At line:1 char:13
    + powershell (Add-Type '[DllImport(\"user32.dll\")]^public static exter ...
    +             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidData: (Microsoft.Power...peCompilerError:AddTypeCompilerError) [Add-Type], Except
       ion
        + FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand
    
    Add-Type : Cannot add type. Compilation errors occurred.
    At line:1 char:13
    + powershell (Add-Type '[DllImport(\"user32.dll\")]^public static exter ...
    +             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidData: (:) [Add-Type], InvalidOperationException
        + FullyQualifiedErrorId : COMPILER_ERRORS,Microsoft.PowerShell.Commands.AddTypeCommand
    

    (Just a note: I've been using Nircmd to do this, it does the same)



  • @Russ Is it possible one of my apps could be preventing it? Such as Wallpaper Engine which creates live wallpapers.



  • @Trenton-Lawson, it's possible because I don't know specifically why it's doing that, but I doubt that's it. It seems more like a hardware problem.



  • @Russ Any idea on what piece of hardware might be causing the issue? If not no worries



  • @Trenton-Lawson, maybe the video card or monitor, but it might even be the motherboard because of what you told me about the keyboard and mouse flashing.



  • NirCMD (https://www.nirsoft.net/utils/nircmd.html) can do this with a single command: nircmd.exe monitor off


Log in to reply