5

I need to be able to turn off my screen instantly manually.

I tried powercfg /change monitor-timeout-ac 0, but looks like setting the value to 0 turns it to never. Values like 0.1 also change it to never.

Is there any way I can set this value to 0 minutes, or is there another command that i can use to turn off my screen manually?

A.Ishah
  • 53

6 Answers6

6

(Running this has the side effect of also locking the workstation in Windows 10. You will need to know the password in order to log back in)

Running in Powershell:

(Add-Type '[DllImport("user32.dll")]public static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam);' -Name a -Pas)::SendMessage(-1,0x0112,0xF170,2)

Would be the correct answer in order to turn off the display. Yes, it also turns off the display on a laptop using Powershell (and some .NET C# code)

riahc3
  • 283
4

Previous solutions work, but they lock powershell input.

Another approach to keep powershell input alive is using PostMessage instead of SendMessage.

(Add-Type "[DllImport(""user32.dll"")] public static extern int PostMessage(int hWnd, int hMsg, int wParam, int lParam);" -Name "Win32PostMessage" -Namespace Win32Functions -PassThru)::PostMessage(0xffff, 0x0112, 0xF170, 2)
1

DisplayConfig is the thing to use !

For other people that would find this post here you can find the workaround :

Install DisplayConfig module : PowerShell Gallery | DisplayConfig

Get your display infos (look at monitor ID) : Get-DisplayInfo

You should get something like this :

PS C:\Users\user1> Get-DisplayInfo

DisplayId DisplayName Active Primary Position Mode ConnectionType --------- ----------- ------ ------- --------


     1 PHL 272V8          True     True 0 0         1920x1080@74,973 Hz      DisplayPort
     2 Mi Monitor         True    False 1920 0      3440x1440@144 Hz         DisplayPort
     3 Philips UHDTV     False    False 0 0                                         HDMI

You can get a list of commands by using Get-Command -Module DisplayConfig

PS C:\Users\user1> Get-Command -Module DisplayConfig

CommandType Name Version Source


Cmdlet Copy-DisplaySource 1.0.5 DisplayConfig Cmdlet Disable-Display 1.0.5 DisplayConfig Cmdlet Disable-DisplayAdvancedColor 1.0.5 DisplayConfig Cmdlet Enable-Display 1.0.5 DisplayConfig Cmdlet Enable-DisplayAdvancedColor 1.0.5 DisplayConfig Cmdlet Get-DisplayColorInfo 1.0.5 DisplayConfig Cmdlet Get-DisplayConfig 1.0.5 DisplayConfig Cmdlet Get-DisplayInfo 1.0.5 DisplayConfig Cmdlet Get-DisplayProfile 1.0.5 DisplayConfig Cmdlet Get-DisplayScale 1.0.5 DisplayConfig Cmdlet Set-DisplayPosition 1.0.5 DisplayConfig Cmdlet Set-DisplayPrimary 1.0.5 DisplayConfig Cmdlet Set-DisplayProfile 1.0.5 DisplayConfig Cmdlet Set-DisplayRefreshRate 1.0.5 DisplayConfig Cmdlet Set-DisplayResolution 1.0.5 DisplayConfig Cmdlet Set-DisplayRotation 1.0.5 DisplayConfig Cmdlet Set-DisplayScale 1.0.5 DisplayConfig Cmdlet Undo-DisplayConfigChanges 1.0.5 DisplayConfig Cmdlet Use-DisplayConfig 1.0.5 DisplayConfig

To Disable any screen use command Disable-Display [ID]

To Enable any screen use command Enable-Display [ID]

0

From this TechNet scripting page:

powershell (Add-Type '[DllImport(\"user32.dll\")]^public static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam);' -Name a -Pas)::SendMessage(-1,0x0112,0xF170,2)
Doug Deden
  • 2,204
  • 1
  • 12
  • 16
0

Tested on Win10 and working

(Add-Type -MemberDefinition "[DllImport(""user32.dll"")]`npublic static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam);" -Name "Win32SendMessage" -Namespace Win32Functions -PassThru)::SendMessage(0xffff, 0x0112, 0xF170, 2)
KERR
  • 624
0

this works perfectly,

powershell -command $obj = Add-Type -MemberDefinition '[DllImport(""""user32.dll"""")] public static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam);' -Name fn -Namespace ns -PassThru; $obj::SendMessage(0xffff, 0x0112, 0xF170, 2)

but after the monitor is turned on PS window remains open. "Tried to add "exit" command, but that didn't work.

Can you help please?

Rohit Gupta
  • 5,096