11

#PSTip How to switch off display with PowerShell

Note: This tip requires PowerShell 2.0 or above.

This week on a PowerShell forum, I stumbled upon user request to turn off his PC’s display on demand. I did not find what was the reason, but I guess he was trying to watch a horror movie and the shining display was ruining the experience. Or, more likely, he wanted to maximize his laptop’s battery life. Either way, after a few web searches I was able to come up with this:

# 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
			);
		}
	}
}
'

The code uses the Add-Type cmdlet to define a new static type Utilities.Display. The type defines public method PowerOff() which you call to power off the display. To try it out run the code above (there will be no output) to define the type and then use this call to power off your display:

[Utilities.Display]::PowerOff()

If it worked for you wrap the method call to Switch-DisplayOff PowerShell function, to make it easier to call and discover.

function Switch-DisplayOff
{
	[Utilities.Display]::PowerOff()
}

Now it is ready to use in your current PowerShell session. If you decide to place it in your $profile for later use make sure you include the whole type definition as well as the function definition.

Filed in: Columns, Tips and Tricks Tags: , , ,

11 Responses to "#PSTip How to switch off display with PowerShell"

  1. Tommy Becker says:

    Once invoked, it doesn’t seem to exit cleanly. I’m running it with POSH 4.0… unless it handles code differently, I don’t see that being a problem… I’ll have to research this.

    But, good job. I can see how you could use this to send code messages anywhere… (mind races) Oh, the places you’ll go!

    • nohandle says:

      Hi, thanks for the comment. I tried it on Powershell 4 (Windows 8 and Windows 8.1) and it acted as I would expect. I executed this: [utilities.display]::poweroff() ; sleep 5 ; ‘test’ Display went black, I waited three seconds, pressed a key, display goes back on and in second the ‘test’string is written to the console. Maybe I am overlooking something. What exactly do you mean by not exiting cleanly?

      • Tommy Becker says:

        Running 4.0 on WIn7 and it freezes my console where I have to ultimately ctrl-break which kills the ps console.

  2. Dennis says:

    What would we have to change to turn the display back on?

  3. nohandle says:

    In my code try substituting the 0x0002 with -1. If that works just rename the methods accordingly. I have currently no option to test it.

  4. Bruno Juchli says:

    I’ve tried with -1 as the value to turn the monitor on.
    Turning it off with 0x0002 works. However, turning it back on with -1 didn’t work.

  5. Clay says:

    Regarding not exiting cleanly (it doesn’t for me, either). I find it interesting that the little utility NirCmd, when used to turn off the screen, won’t “exit cleanly” when invoked from a cmd prompt either; in its case, I have to ctrl-c to exit – though it too works. Makes me think it’s related to specific hardware. (I’m using a Lenovo laptop, Win 10.) Though I am in the *shrug* but-what-does-he-know user class.

    I’m very new at this. Wish I knew enough to turn your code into a nice, safe little icon that reliably shut off my blasted screen on demand.

Leave a Reply

Submit Comment

© 2016 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: