How does one use PWSH to turn off physical display despite being remoted into Windows 10?

Copper Contributor

The below PWSH code works wonderfully to keep a physical display turned off if one is physically in front of a Windows 10 device and logged in (displays turning on spuriously is a totally different problem but hey, one thing at a time).


However, what I'd like to do is run a script so that when one is remoted into a Windows 10 device, the physical display remains off (although the remote desktop display is still visible in the remote desktop client), regardless of initially making the remote desktop connection, maintaining the connection, or disconnecting from the remote session for any reason.


Also, please no non-answer responses (e.g. "Why don't you just do XYZ instead?", "I haven't tested it myself but, 'BLAH BLAH BLAH'.", "Why do you want to do that?", etc.).


Thanks!


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
);
}
}
}
'
while($true){
[Utilities.Display]::PowerOff()
$null=Start-Sleep -m 4500
}

0 Replies