-
-
Save oledid/fb02951b6b1848d1418d to your computer and use it in GitHub Desktop.
# Source: http://www.powershellmagazine.com/2013/07/18/pstip-how-to-switch-off-display-with-powershell/ | |
# Turn display off by calling WindowsAPI. | |
# SendMessage(HWND_BROADCAST,WM_SYSCOMMAND, SC_MONITORPOWER, POWER_OFF) | |
# HWND_BROADCAST 0xffff | |
# WM_SYSCOMMAND 0x0112 | |
# SC_MONITORPOWER 0xf170 | |
# POWER_OFF 0x0002 | |
Add-Type -TypeDefinition ' | |
using System; | |
using System.Runtime.InteropServices; | |
namespace Utilities { | |
public static class Display | |
{ | |
[DllImport("user32.dll", CharSet = CharSet.Auto)] | |
private static extern IntPtr SendMessage( | |
IntPtr hWnd, | |
UInt32 Msg, | |
IntPtr wParam, | |
IntPtr lParam | |
); | |
public static void PowerOff () | |
{ | |
SendMessage( | |
(IntPtr)0xffff, // HWND_BROADCAST | |
0x0112, // WM_SYSCOMMAND | |
(IntPtr)0xf170, // SC_MONITORPOWER | |
(IntPtr)0x0002 // POWER_OFF | |
); | |
} | |
} | |
} | |
' | |
[Utilities.Display]::PowerOff() |
Works like a charm.
The script doesn't seem to terminate tho - how do I close the window automatically after it turned the display(s) off?
@TrueMilli how do you run the script?
@oledid In Powershell or PowerShell ISE, doesn't matter. My workaround is to call it from a wrapper:
$process = Start-Process -FilePath $PSScriptRoot\DisplayOffMethod.ps1 -PassThru
Start-Sleep 3
Stop-Process $process
@TrueMilli great that you found a workaround.
@TrueMilli found an even better solution. Replace SendMessage with PostMessage, PostMessage work asynchronous and does not cause the script to hang!
@oledid you might want to replace that in your snippet if you find that appropriate.
@PCHSwS I don't maintain the script unless I need to use it again, but people can see your comments here - and here's even a link to your fork: https://gist.github.com/PCHSwS/39e213aac7c574b89673da8dc575e7fa
Is there a possibility to pick which from X monitors we can disable?
Thanks mate