Skip to content

Instantly share code, notes, and snippets.

@jbinfo
Created September 18, 2025 16:06
Show Gist options
  • Save jbinfo/90faaaf1c3b4eaddd214d1db9954b44f to your computer and use it in GitHub Desktop.
Save jbinfo/90faaaf1c3b4eaddd214d1db9954b44f to your computer and use it in GitHub Desktop.
Stay Active – PowerShell Script to Prevent Windows Idle Lock
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class NativeMethods {
[DllImport("user32.dll")]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData, UIntPtr dwExtraInfo);
}
"@
function Move-Mouse {
# Get current mouse position
Add-Type -AssemblyName System.Windows.Forms
$pos = [System.Windows.Forms.Cursor]::Position
$x = $pos.X
$y = $pos.Y
# Move slightly (+1,+1)
[NativeMethods]::mouse_event(0x0001, 1, 1, 0, [UIntPtr]::Zero)
Start-Sleep -Milliseconds 100
# Move back to original position
[System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point($x, $y)
}
while ($true) {
$hour = (Get-Date).Hour
if ($hour -ge 8 -and $hour -lt 18) {
Move-Mouse
}
Start-Sleep -Seconds 60
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment