-
-
Save EugeneLoy/150044d04b08e35d09e164c864e78da7 to your computer and use it in GitHub Desktop.
powershell (Add-Type '[DllImport(\"user32.dll\")]^public static extern int PostMessage(int hWnd, int hMsg, int wParam, int lParam);' -Name a -Pas)::PostMessage(-1,0x0112,0xF170,2) |
@Ma-XX-oN This piece of code originally came from stackoverflow. I copied it years back and I can't quite remember the source post. Anyway, it "just did the job" for me back then and I didn't give it much thought. If someone uses this, it is probably a good idea to take note of your comment.
Well, if anyone wishes to have that code, here it is:
powershell (Add-Type '[DllImport(\"user32.dll\")]^public static extern int PostMessage(int hWnd, int hMsg, int wParam, int lParam);' -Name a -Pas)::PostMessage(-1,0x0112,0xF170,2)
I found that using SendMessage
resulted in the command to stall because it was waiting for a response.
@Ma-XX-oN Thanks. I've updated the gist with your code.
is there a way to tell it to which of the multimonitor setup to turn off ?
Not that I'm aware of. AFAIK, it just turns off all of them.
Very nice. I dropped it into my scripts folder and placed a shortcut on my desktop. I already had something similar on Linux and I am glad to see someone already did this for Windows.
Great script! Just a note, .NET 3.5 is required on Windows 11 for this to function. Otherwise you'd get mscoree.dll
error.
Great script! Just wanted to add this here in case someone needs it, if you want the screen shutdown to be delayed by a certain amount of time after you run the .bat file, you can add this before the .bat file code:
@echo off
timeout /t x >nul
x is the number of seconds you want the delay for. Really helpful if you use chrome remote desktop :-)
Any way to ensure that the screen is turned back on only when the mouse is clicked and not when moved?
I am not aware of a windows setting for that. Any activity will bring it back. If there is YouTube video playing in the background, it can bring the screen back on too. So, if you want to just listen, open another tab in your browser so that the YouTube video is not in the active tab.
Hmm, during times when listening to some audio on the computer and turning the screen off, it works and isn't turned back on.
Hopefully there are ways to turn the screen back on only when there is certain event - in this case a mouse click.
Hmm, during times when listening to some audio on the computer and turning the screen off, it works and isn't turned back on. Hopefully there are ways to turn the screen back on only when there is certain event - in this case a mouse click.
Did you ever find a solution to this? Want to do the same thing. Thanks!
This shuts off the screen and locks the desktop. Would be nice if there was a way to just shutoff the screen.
AFAIK, the Windows OS will wake the screen on any KB or mouse event, and will bring you to the unlock screen (or desktop if security is disabled).
Is this still working for you guys? for me the screen turns off then immediatley turns back on - no mouse or keyboard movements (by me)...
@Shazib
Can confirm that it works for me with Windows 10 Pro 22H2.
First of all thanks to EugeneLoy and unknown for me the author of the command :-)
Today 2.6.2023 - it works for me. Conditions - Win10 22H2. Two monitors. Both displays are turned off one time.
Is this still working for you guys? for me the screen turns off then immediatley turns back on - no mouse or keyboard movements (by me)...
Cheers.
FYI I've discovered that this doesn't play nice with my monitor that supports DDC/CI control. https://github.com/xanderfrangos/twinkle-tray/ works though.
Thank you
To anyone looking to shut off a specific monitor and have it stay off until you physically push the monitor power button, it looks like there is a utility for it. Can even change settings by command line.
https://www.winhelponline.com/blog/turn-off-monitor-shortcut-command-windows/
Nirsoft’s ControlMyMonitor
Brilliant, thanks for this. Windows is a bit weird when you mix and match displayport and HDMI monitors where windows will jump from the DP monitor to the HDMI one when you physically turn the power off both, so this solution with a bat file is preferable - even thought I also use Persistent Windows.
FYI I've discovered that this doesn't play nice with my monitor that supports DDC/CI control. https://github.com/xanderfrangos/twinkle-tray/ works though.
Also, thanks for the recommendation of Twinke-Tray @Shazib.
I'm having a problem using this powershell script on my Asus MiniPC PB60. It does turn off the monitor. I set it up in Task Scheduler to run when the machine is locked. Works like a charm. Trouble is that it seems to disable background processes, too. I'm using this machine primarily as an iTunes and HomeBridge server. But if I use this script, both HomeBridge and iTunes stop working with the machine locked. It also takes out the little utility that controls the UPS, which is connected by USB. As long as I don't use this script to turn off the monitor, everything works fine with the machine locked.
Any ideas? Is there a way to modify this script to turn the monitor off, but allow everything else to keep running?
I'm having a problem using this powershell script on my Asus MiniPC PB60. It does turn off the monitor. I set it up in Task Scheduler to run when the machine is locked. Works like a charm. Trouble is that it seems to disable background processes, too. I'm using this machine primarily as an iTunes and HomeBridge server. But if I use this script, both HomeBridge and iTunes stop working with the machine locked. It also takes out the little utility that controls the UPS, which is connected by USB. As long as I don't use this script to turn off the monitor, everything works fine with the machine locked.
Any ideas? Is there a way to modify this script to turn the monitor off, but allow everything else to keep running?
It seems like this is an issue with modern standby modes. When you turn the monitor off in this way it puts the entire machine into sleep mode. I'm guessing that this only happens on devices such as laptops.
There is a stack overflow post here discussing it, and offering some recommendations for alternative API's you can call to resolve
Why is this using
SendMessage
? Wouldn'tPostMessage
be a better fit as you don't have to wait for a response, especially since you're sending to all top level windows, where all of which may not play nice?