Running a script from task scheduler

its a simple script to prevent windows locking. I want the script to un at startup using task scheduler. if i copy and paste it into PS it works, the script is in a notepad with extension .ps1

param($minutes = [double]::PositiveInfinity)

$myshell = New-Object -com “Wscript.Shell”

Echo “ScrollLock spam is running to stop press Ctrl + C”

for ($i = 0; $i -lt $minutes; $i++) {

Start-Sleep -Seconds 30

$myshell.sendkeys(“{SCROLLLOCK}”)

}

Hi, welcome to the forum. You should always use the codeblock formatting button when sharing code, it makes it much easier to read:

param($minutes = [double]::PositiveInfinity)

$myshell = New-Object -com “Wscript.Shell”

Echo “ScrollLock spam is running to stop press Ctrl + C”

for ($i = 0; $i -lt $minutes; $i++) {

Start-Sleep -Seconds 30

$myshell.sendkeys(“{SCROLLLOCK}”)

}

The reason this doesn’t work is because the code is sending a keypress as a way to keep the computer awake, but when you have Task Scheduler execute PowerShell for you it does it in a separate session and likely not YOUR desktop session. I’d have to play with my keep-alive script and task scheduler a bit but I’m pretty sure that’s the issue you’re running in to.

EDIT: do you have your scheduled task configured to run at startup or login? And what’s the principal (user) you have it running as?

I’d add to this that, i don’t think using PS and task scheduler is the right move here.

You could just configure windows to not go to lock screen? if you google it, there’s going to be a ton of hits. If you’re on a system where you can’t configure that then you probably shouldn’t be trying to circumvent it :slight_smile:

2 Likes