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.
A couple more options:
nircmd monitor off from the command line. More information at the link.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
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.
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)
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.
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)"
Here's one I tried on windows and it worked. Puts monitor to sleep only. Turns on mouse hover. Very useful . After trying couple others this is the easiest I can say for sure.
Using NirCmd.
Once installed
create a new shortcut on your desktop
In the location field, type:
C:\Path\To\nircmd.exe monitor off
(Replace C:\Path\To\ with the actual path where you saved nircmd.exe.)
Name the shortcut (e.g., "Turn Off Screen") and you're done.
Download link ️ https://www.nirsoft.net/utils/nircmd.html#google_vignette