Keep awake PS Script

PowerShell Script

Keep awake PowerShell script

Many companies with stringent access requirements for their corporate networks often opt to allocate two separate machines or laptops to their technical staff. In this setup, one device is generally designated for tasks like research, email correspondence, and general web browsing related to work. Meanwhile, the second system remains isolated from external access and serves as a pristine, secure environment.

To address a common concern when using such setups, I've developed a script that prevents one of these machines from entering the screensaver or sleep mode. This script can be exceptionally beneficial when engaging with textual content, configuration files, or diagrams that demand continuous display without interruptions.

This will work without Administrator access or UAC popups.

param (
    [int]$minutes = 120
)

# Validate parameter
if ($minutes -le 0) {
    Write-Host "Invalid parameter value. Please provide a positive integer."
    exit
}

try {
    # Import the assembly for SendKeys
    Add-Type -AssemblyName System.Windows.Forms

    # Create an instance of SendKeys
    $sendKeys = [System.Windows.Forms.SendKeys]

    for ($i = 0; $i -lt $minutes; $i++) {
        Start-Sleep -Seconds 45
        $sendKeys::SendWait(".")
    }
} catch {
    Write-Host "An error occurred: $_"
}

# Set execution policy (if needed)
if (!(Get-ExecutionPolicy -Scope CurrentUser) -eq "RemoteSigned") {
    Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
}

Another revised version that works on more restricted systems. (This isn't my own code).

param($minutes = 60)

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

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

Save this as something.PS1 and run this from PowerShell like .\something.ps1

Previous Post Next Post