Skip to content

Instantly share code, notes, and snippets.

@konstunn
Last active March 21, 2017 17:11
Show Gist options
  • Save konstunn/05e0506400116c49c15f4f753f6da3ac to your computer and use it in GitHub Desktop.
Save konstunn/05e0506400116c49c15f4f753f6da3ac to your computer and use it in GitHub Desktop.
PowerShell script to power off display and lock workstation in a couple of clicks or a hotkey stroke. (Found somewhere on the Web.)
# 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
);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern bool LockWorkStation();
public static void PowerOff()
{
SendMessage(
(IntPtr)0xffff, // HWND_BROADCAST
0x0112, // WM_SYSCOMMAND
(IntPtr)0xf170, // SC_MONITORPOWER
(IntPtr)0x0002 // POWER_OFF
);
LockWorkStation();
}
}
}
'
[Utilities.Display]::PowerOff()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment