Home > Net >  Using PowerShell to send space character to desktop - to wake screen from sleep
Using PowerShell to send space character to desktop - to wake screen from sleep

Time:02-11

I know the following 2 line PowerShell script sends a space character to a new "shell" object it creates. Which does bring my sleeping screen out of its slumber. But that opens, momentarily, a PowerShell window. So, if the screen is truly asleep-not a problem.

But, there are times of the day when this will run when the screen isn't in a sleeping slumber. Hence, when that happens, the user sees a very short annoying flash of the created Wscript.Shell

I'd like to just send the space keystroke to the desktop - to wake the sleeping screen. Without the momentary opening and closing of the Wscript.Shell i.e., send the " " character to "the desktop" and not to a new shell object. Is this possible? I know nothing (obviously) about writing PowerShell.

$myshell=New-Object -com "Wscript.Shell" 
$myshell.sendkeys(" ")

Perhaps there is a way the PowerShell script could know if the screen and/or computer is not in a sleep state, and therefore this could all just be avoided from running in that situation? Any help is much appreciated.

CodePudding user response:

It's being setup with a Powershell script to create a Task in task scheduler. That task then calls the 2 line PowerShell script to send the " " key, to wake the computer up just prior to the day's activity.

CodePudding user response:

I found my answer. Since I am using AutoHotKey, the following code will use PowerShell to create a new task in task scheduler. By using the cmd /c start /min "" powershell ... style of command, the pop-up window does not display - but the computer and screen wake up.

WakeUp:= A_Now
EnvAdd WakeUp, 10, Minutes       ; wake 10 minutes from now
UTC_Delta-= A_NowUTC, Seconds    ; Seconds is more accurate due to rounding issue
UTC_Delta:= Round(-UTC_Delta/60) ; Round to nearest minute to ensure accuracy
WakeUp    = UTC_Delta, Minutes   ; Apply offset to convert to UTC
FormatTime, WakeTime,% WakeUp, "yyyy-MM-ddTHH:mm:ssZ"
psScript =
(
$action = New-ScheduledTaskAction -Execute cmd -Argument '/c start /min "" powershell -WindowStyle Hidden -ExecutionPolicy Bypass -command {$myshell=New-Object -com "Wscript.Shell" $myshell.sendkeys(" ")}'
$settings = New-ScheduledTaskSettingsSet -WakeToRun
$trigger = New-ScheduledTaskTrigger -Once -At %WakeTime%
Register-ScheduledTask -TaskName wakeup -Action $action -Settings $settings -Trigger $trigger -Force
)
RunWait PowerShell.exe -Command &{%psScript%} ,, hide
;use the following if you want to see PowerShell output
;Run PowerShell.exe -NoExit -Command &{%psScript%}
  • Related