Skip to content

Instantly share code, notes, and snippets.

@prasannavl
Created September 26, 2015 21:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prasannavl/835ce9882a9357487df4 to your computer and use it in GitHub Desktop.
Save prasannavl/835ce9882a9357487df4 to your computer and use it in GitHub Desktop.
Powershell script to turn display off
# 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 PVL {
public static partial class WindowsAPI
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr PostMessage(
IntPtr hWnd,
UInt32 Msg,
IntPtr wParam,
IntPtr lParam
);
}
public static partial class Utility
{
public static partial class Display
{
public static void PowerOff ()
{
WindowsAPI.PostMessage(
(IntPtr)0xffff, // HWND_BROADCAST
0x0112, // WM_SYSCOMMAND
(IntPtr)0xf170, // SC_MONITORPOWER
(IntPtr)0x0002 // POWER_OFF
);
}
}
}
}
'
function Set-DisplayOff {
[PVL.Utility+Display]::PowerOff();
}
@sainiaj
Copy link

sainiaj commented Mar 11, 2018

Hi can you please make opposite of this script,
i am currently using this script to turn off the monitor (task scheduler) @ 5:01pm
and have another script to reboot machine at (task scheduler) @8:59am - shutdown /r

this computer does not have keyboard or mouse attached thats why need an script
Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment