Keeping A PC Awake and Unlocked

Keeping A PC Awake and Unlocked

There are times when I really, really hate the auto-sleep feature on my computers. Because having a machine lock itself when I am monitoring something is a major pain in the patootie. So I came up with a way to keep the machine awake and unlocked.

I’m not sure how many people have this problem. I find that it is an issue for me both at home and at work. Here are the scenarios:

  • Home: I was trying to copy files from my old laptop to my new. But the old laptop would go to sleep after 30 minutes, and the copy would stall.
  • Work: I was running something on my desktop machine while working on my laptop. The desktop machine would lock itself after 3 minutes, meaning I had to put my password in each time I wanted to check on the progress.

Autolocking and Sleep are Good

I understand why it is good to let my home laptop go to sleep after 30 minutes. It doesn’t need to be turned on all the time, and heaven knows I would never shut it off.

I understand why my client auto-locks machines after 3 minutes of inactivity. There is less chance of someone unauthorized finding an unlocked machine and working at it.

But given that I am doing something active at home, and literally sitting next to the machine at work, these restrictions are frustrating.

So I Made A Solution

I’m a programmer, and since these problems are on computers, it was easy to make a solution.

It’s a PowerShell script, and it simply simulates a key press every 60 seconds. The script runs for 10 hours.

Here is the code:

param($minutes = 600)

$myshell = New-Object -com "Wscript.Shell"

for ($i = 0; $i -lt $minutes; $i++) {
	Start-Sleep -Seconds 60
	$myshell.sendkeys("{F13}")
}

Explanation

The first line sets the number of minutes that the script will execute. In this case 10 hours (600 minutes)

Every 60 seconds, the script will send the F13 key to the computer. You may be asking “what is the F13 key?” It doesn’t exist. But the computer doesn’t know that. ?

Using the Script

To use the script:

  1. Copy the above code into a new Notepad document.
  2. Save the file with a PS1 extension. I called mine KeepLive.ps1
  3. Right-click on the saved file and choose “Run with PowerShell”

Disclaimers

Of course, only use this if it is eligible to be used under your company’s policies. Always understand code that you run from the Internet. I cannot confirm that this script can make Microsoft teams think you are at your desk and available. Use this script at your own risk. I and my blog cannot be held responsible for the use of the script.