100

Is there a way to turn off the display in Windows (7), preferably without using additional software?

Powershell script works fine, but leaves command-line window after turning on the display.

Ƭᴇcʜιᴇ007
  • 111,883
  • 19
  • 201
  • 268
unicoio
  • 1,009
  • 2
  • 8
  • 3
  • 2
    You have an existing ps1 script? They don't usually leave the window open after running. Perhaps there's something in the script that needs to be changed to let the window close? – Ƭᴇcʜιᴇ007 Aug 10 '11 at 13:16
  • 6
    also if you may paste the ps script here - it will be useful for us visitors – Ujjwal Singh Oct 10 '12 at 19:27
  • 1
    How about using the blank screen saver plus setting your power settings to turn off the monitor after some period of non-use? – uSlackr Aug 10 '11 at 13:47
  • 1
    Put PowerShell command from below answers in a file, save as .ps1, chose default app to open with as powershell, create a shortcut for it, go to the properties of the shortcut and set up a key combination to trigger it. this key combination will work from anywere. – Shankara Narayana Jul 11 '21 at 00:51
  • might have to place the shortcut in C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Accessories for the shortcut keystore to work. – Shankara Narayana Jul 11 '21 at 01:00

6 Answers6

55

A couple more options:

  • Turn Off LCD - just place the executable on your desktop
  • NirCmd - you'll need to copy nircmd.exe to your Windows system directory, or add its location to the PATH environment variable. Then just run nircmd monitor off from the command line. More information at the link.
Indrek
  • 24,204
  • 14
  • 90
  • 93
duykhoa
  • 651
  • 5
  • 5
54

On a laptop you can use the keyboard shortcut combination of Fn+F7 (F7 might differ depending on the laptop model) and for a desktop you can always use the power button.

Do you need any other specifications such as wake up on mouse movement or something else?

You can always create a shortcut and assign a keyboard shortcut to a black screensaver, use this path:

%systemroot%\system32\scrnsave.scr /s

This will not turn off you screen but make it completely black

Greg
  • 4,262
  • 2
  • 20
  • 19
  • 2
    I have desktop and as far as I know it is not possible to set power button to turn off display. Wake up on mouse movement or keyboard key is fine. Black screensaver leaves glow, which is visible at night. – unicoio Aug 10 '11 at 12:21
  • 3
    @unicoio If it's to turn off the screen for the whole night why don't you use the power button on the screen itself? – Greg Aug 10 '11 at 13:04
  • 1
    I arrived at this question over three whole steps, literal strides. Your screensaver answer was exactly what I needed. My LCD TV treats solid complete black as off. – Motomotes Mar 26 '14 at 02:57
  • +1 for launching scrnsave.scr. This doesn't kick in the actual power save, so I can attach it to a hotkey and turn the laptop monitor off while lid remains open. – LMSingh Aug 04 '14 at 01:49
  • 2
    What's `/s` for, it works without the switch as well. –  Oct 02 '16 at 23:29
  • @Chinggis6 no, it does not (or it **might**, but it should not really). As [microsoft's docs say](https://support.microsoft.com/en-us/kb/182383) `/s` stands for "Run the Screen Saver now", while launching w/o any switch means "Show the Settings dialog box" – Marcin Orlowski Oct 30 '16 at 19:37
  • 5
    In Windows 10, screen gets black when `scrnsave.scr` is entered in the search box or in run dialog, but it requires `/s` when executed from `cmd.exe` (without it, a dialog pops up informing that this screensaver has no configurable options). – Palec Jun 08 '17 at 01:22
43

You can use WinAPI call SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 2) where HWND_BROADCAST = 0xFFFF, WM_SYSCOMMAND = 0x0112 and SC_MONITORPOWER = 0xF170. The 2 means the display is being shut off.

There are several ways to make the call:

  • Separate executable. You can fire it through a script, command line, Run window, shortcut (*.lnk), etc. Note that shortcuts can be invoked using a keyboard shortcut. The executable may be written in C or C++, or via P/Invoke in .NET languages (C# or PowerShell), or in many other languages that have a foreign language interface (e.g. JNI in Java).

  • AutoHotkey script. For a non-programmer, this way is probably simpler. Making customizations still requires some scripting. This script turns monitor off on Win + M:

    #m::
    Sleep 1000
    SendMessage, 0x112, 0xF170, 2,, Program Manager
    return
    

Note the timeout before the SendMessage call in the AutoHotkey script. It gives the user a chance to release keys (in case their release would wake up the monitor again). Do not forget about it even when making the call from a script in another language.

For more info, see the documentation of SendMessage function, WM_SYSCOMMAND message and AutoHotkey SendMessage. It might be of interest that since Windows 8, using the same method to turn monitor on does not work, but there is a work-around.

Palec
  • 486
  • 5
  • 20
Ujjwal Singh
  • 2,298
  • 1
  • 23
  • 27
  • For the AutoHotkey method, I would recommend using a `Sleep 50` **after** `SendMessage` instead of this long sleep before. This has the effect of turning off the screen immediately. Tested on my own laptop and found it to be better. – cyqsimon Aug 17 '20 at 18:25
20

Powershell one-liner would be:

(Add-Type -MemberDefinition "[DllImport(""user32.dll"")]`npublic static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam);" -Name "Win32SendMessage" -Namespace Win32Functions -PassThru)::SendMessage(0xffff, 0x0112, 0xF170, 2)
Jari Turkia
  • 647
  • 8
  • 13
  • 4
    This was close, but didn't work for me (Win10). However I put this into a .bat file and it worked: `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)` – Kidquick Dec 15 '20 at 03:04
  • 1
    Well adapted! I'm a PowerShell user and would run from a `.ps1`-file. Running unsigned PowerShell-scripts isn't enabled by default, so your solution will work for all. – Jari Turkia Dec 15 '20 at 10:05
  • 1
    This does not work on my Windows 10 20H2 computer. The one-liner will hibernate the entire computer. Same problem with NirCMD. The one-liner and NirCMD work on Windows 7 computer. – JPX Jan 13 '21 at 15:26
  • On my Win10 20H2 running above single-liner in PowerShell Core 7.1 does turn off display as it did in previous versions. It does not hibernate my computer. – Jari Turkia Jan 13 '21 at 19:13
  • 1
    I tested the single-liner and NirCMD in two Lenovo ThinkCentre M90N-1 computers which will hibernate. Windows turns off the monitor without hibernation. If I try to turn the display on by running a command `nircmd monitor on`, monitor will turn on for a second and then the computer will hibernate. – JPX Jan 13 '21 at 19:33
  • 2
    I've tested the PowerShell single-liner on couple of my computers. Working as as expected. Given you're the first one to report computer hibernation, I'd have to assume this is a Lenovo-issue. – Jari Turkia Jan 14 '21 at 07:02
  • This is Kidquick's script in raw powershell: ```(Add-Type "[DllImport(""user32.dll"")]`npublic static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam);" -Name a -Pas)::SendMessage(-1,0x0112,0xF170,2)``` – CeePlusPlus Feb 11 '21 at 02:47
  • 1
    @CeePlusPlus Kidquick's script is exactly the same when compensated for missing names for arguments and -1 vs 0xffff. – Jari Turkia Feb 11 '21 at 15:55
  • I hear, its not immediately clear with `MemberDefinition` and `-Namespace Win32Functions -PassThru`. I'm looking to turn off 1 monitor of 3 if you have any suggestion by the way https://superuser.com/questions/1625092/script-to-disable-3rd-monitor-in-windows-10 – CeePlusPlus Feb 11 '21 at 18:31
  • 1
    This solution is nice, but it has the drawback of also stopping any music player (tested with Potplayer and VLC), at least on my Thinkpad Yoga. It seems the command induces a general low-power state (though not hibernation in my case) rather than just turning off the monitor. – pglpm Feb 16 '21 at 07:26
  • 1
    Something similar to hibernate or music stopping, on a HP Z-book laptop I tested, running the one-liner will enable screen lock. When displays are turned on, a login is required. – Jari Turkia Feb 22 '21 at 07:38
  • 1
    I confirm, on a laptop Asus Rog it also turn off the display but also stop any current network activities... so it doesn't only affect the screen, but more turn the pc in power saving mode... (if there is any news about this subject I would be interested :) ) – 8HoLoN Oct 17 '21 at 15:11
  • 1
    The same result occurs on an Acer swift 7 pro (network disabled). So it's not brand related. – 8HoLoN Oct 17 '21 at 19:09
  • 1
    The lib call (and `nircmd display off`) both not only turns off a display but also makes Windows 10 suspend (no disk/network/audio activity) with a ThinkPad laptop. Probably this is related to battery power plan or Lenvo Vintage bloatware. I reduced display backlight to save power... – gavenkoa Nov 30 '22 at 10:18
  • 1
    @gavenkoa Look at the comments from JPX. Display off can trigger other effects. Those are not easily reproduced on a random Windows 10 or 11. I've tested the above one-liner on dozen PCs without any adverse effects. – Jari Turkia Nov 30 '22 at 11:34
  • Winner - this one turns the screen off instead of just blanking it like the "scrnsave.scr /s" answer! – KERR Dec 15 '22 at 13:06
3

As one-liner for instance using Win+R:

powershell -command $obj = Add-Type -MemberDefinition '[DllImport(""""user32.dll"""")] public static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam);' -Name fn -Namespace ns -PassThru; $obj::SendMessage(0xffff, 0x0112, 0xF170, 2)

It's not possible to put in a shortcut .lnk file unfortunately because the line is too long, I run it with Flow Launcher and my Favorites Plugin, this supports long command lines.

https://github.com/stax76/Flow.Launcher.Plugin.Favorites

stax76
  • 141
  • 4
  • 2
    i can create a shortcut with this command without any problem: `powershell (Add-Type '[DllImport(\"user32.dll\")]public static extern int PostMessage(int h,int m,int w,int l);' -Name a -Pas)::PostMessage(-1,0x0112,0xF170,2)` [check this](https://mybyways.com/blog/command-to-turn-off-monitor-in-windows-10) – S.Serpooshan Feb 22 '23 at 20:21
1

Based on Ujjwal Singh's answer, here is a Python script to do so.

First install pywin32 module, if you haven't already.

$ python -m pip install pywin32

Here is the script:

from win32api import SendMessage

SendMessage(
    # https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-sendmessage?redirectedfrom=MSDN#parameters
    0xFFFF,  # HWND_BROADCAST
    # https://docs.microsoft.com/en-us/windows/win32/menurc/wm-syscommand
    0x0112,  # WM_SYSCOMMAND
    0xF170,  # SC_MONITORPOWER
    2,  # the display is being shut off
)

Or if you prefer a one-liner to run directly from command prompt:

$ python -c "import win32api; win32api.SendMessage(0xFFFF, 0x0112, 0xF170, 2)"
Dr. Gut
  • 386
  • 3
  • 13
AXO
  • 948
  • 12
  • 19