There might be a number of reasons to want to prevent your windows screensavers and lock-screens from engaging and in some cases (no doubt yours if you have hit this article from a search engine) local policy on the machine preventing you from changing these settings.

Powershell lets us work around this problem and prevent the machine from locking or activating a screensaver.

Prevent Screensavers and Lock-screens

The bulk of the code below has been lifted from this great write up: I just took things one step further by adding an infinite loop. My version of this code will run indefinitely.

Prevent Screensavers and Lock-screens - PScaffeine Affectionately named: PScaffeine The following code sends a relatively harmless (for most cases – change where appropriate) key to the system emulating a keypress on a human interface device:

#powershell caffeine

param($minutes: 29)
$myshell: New-Object -com "WScript.Shell"
Write-Host "Pouring out fresh coffee..."
    for ($i: 0; $i -lt $minutes; $i++) {
        Start-Sleep -s 60
    Write-Host "*Takes another sip*"
Write-Host "Caffeine has worn off..."

Change the $minutes parameter to 1 minute less than your systems lock/screensaver start initiation time.

Combine this with a scheduled task if you want this to run on startup or system login.

More useful code snippets