timsstuff

Set-Monitor.ps1

Jul 28th, 2025
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 1.32 KB | Source Code | 0 0
  1. [CmdletBinding()]
  2. param(
  3.     [Parameter(Mandatory=$false)][switch]$PowerOn
  4. )
  5.  
  6. # Turn display off by calling WindowsAPI.
  7.  
  8. # SendMessage(HWND_BROADCAST,WM_SYSCOMMAND, SC_MONITORPOWER, POWER_OFF)
  9. # HWND_BROADCAST  0xffff
  10. # WM_SYSCOMMAND   0x0112
  11. # SC_MONITORPOWER 0xf170
  12. # POWER_OFF       0x0002
  13.  
  14. Add-Type -TypeDefinition '
  15. using System;
  16. using System.Runtime.InteropServices;
  17.  
  18. namespace Utilities {
  19.   public static class Display
  20.   {
  21.      [DllImport("user32.dll", CharSet = CharSet.Auto)]
  22.      private static extern IntPtr SendMessage(
  23.         IntPtr hWnd,
  24.         UInt32 Msg,
  25.         IntPtr wParam,
  26.         IntPtr lParam
  27.      );
  28.  
  29.      public static void PowerOff ()
  30.      {
  31.         SendMessage(
  32.            (IntPtr)0xffff, // HWND_BROADCAST
  33.            0x0112,         // WM_SYSCOMMAND
  34.            (IntPtr)0xf170, // SC_MONITORPOWER
  35.            (IntPtr)0x0002  // POWER_OFF
  36.         );
  37.      }
  38.  
  39.      public static void PowerOn ()
  40.      {
  41.         SendMessage(
  42.            (IntPtr)0xffff, // HWND_BROADCAST
  43.            0x0112,         // WM_SYSCOMMAND
  44.            (IntPtr)0xf170, // SC_MONITORPOWER
  45.            (IntPtr)(-0x0001)  // POWER_ON
  46.         );
  47.      }
  48.   }
  49. }
  50. '
  51.  
  52. Start-Sleep 5
  53. if($PowerOn) {
  54.    [Utilities.Display]::PowerOn()
  55. } else {
  56.    [Utilities.Display]::PowerOff()
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment